﻿using BusinessRepository;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BusinessEntity;
using BusinessEntity.Assets;
using BusinessEntity.QuerryEntity;
using BusinessRule.Organization;
using APICommond;

namespace BusinessRule
{
    public class Assets
    {
        #region "测试"
        public static async Task<(DataTable, int)> GetAssetsList(string scid, string uid, object param, string orderField, bool isAsc, int Size, int pageIndex)
        {
            //return userService.GetEntity(keyValue);


            string strsql = "";
            if (scid == "020010")
            {
                strsql = "select * from Tb_PH_Assets";
            }
            else
            {

            }

            return await new RepositoryFactory().BaseRepository().FindTable(strsql, param, orderField, isAsc, Size, pageIndex);
        }

        public static async Task<(DataTable, int)> GetMainList(MainListEntity entity)
        {
            string strsql = "";
            switch (entity.screenID)
            {
                case "020010":
                    strsql = "select * from Tb_PH_Assets";
                    break;
                case "020020":
                    strsql = "select * from TB_PH_AssetsDetail";
                    break;
            }
            bool isAsc = true;
            if (entity.sort != "Asc")
            {
                isAsc = false;
            }
            if (entity.psWhere != "")
            {
                strsql += " " + entity.psWhere;
            }
            return await new RepositoryFactory().BaseRepository().FindTable(strsql, null, entity.sortString, isAsc, entity.Size, entity.Current);
        }
        #endregion

        #region "数据字典"

        public static async Task<(DataTable, int)> GetItems(string item, int Size, int Current)
        {
            string sql = "select * from ZC_ZDLX";
            if (item != "" & item != null)
            {
                sql += " where item='" + item + "'";
            }
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null, "Item", true, Size, Current);
        }

        public static async Task InsertItem(ZC_ZDLX zd)
        {
            await new RepositoryFactory().BaseRepository().Insert(zd);
        }

        public static async Task<DataTable> GetZC_SJZDTable(string Item)
        {
            string sql = "select Item,Flag,FlagValue from ZC_SJZD where Item='" + Item + "'";
            DataTable data = await new RepositoryFactory().BaseRepository().FindTable(sql, null);
            return data;
        }

        public static async Task<DataTable> GetZC_SJZDByID(string Item, string Flag)
        {
            string sql = "select * from ZC_SJZD where Item='" + Item + "' and Flag='" + Flag + "'";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }

        public static async Task<List<ZC_SJZDList>> GetZC_SJZD(string Item)
        {
            string sql = "select * from ZC_SJZD where Item='" + Item + "'";
            DataTable data = await new RepositoryFactory().BaseRepository().FindTable(sql, null);
            List<ZC_SJZDList> list = new List<ZC_SJZDList>();
            foreach (DataRow row in data.Select("ParentID=''"))
            {
                ZC_SJZDList zds = new ZC_SJZDList();
                ZC_SJZD zd = zds.SysDictionary;
                zd.Item = Convert.ToString(row["Item"]);
                zd.Flag = Convert.ToString(row["Flag"]);
                zd.FlagValue = Convert.ToString(row["FlagValue"]);
                zd.ParentID = Convert.ToString(row["ParentID"]);
                zd.IsUse = Convert.ToString(row["IsUse"]);
                List<ZC_SJZDList> zdss = await CreateZDlist(data, zd.Flag);

                if (zdss != null)
                {
                    zds.SysDictionaryList = zdss;
                }
                list.Add(zds);
            }
            return list;
        }

        private static async Task<List<ZC_SJZDList>> CreateZDlist(DataTable table, string Flag)
        {
            DataRow[] rows = table.Select("ParentID='" + Flag + "'");
            if (rows.Length == 0)
            {
                return null;
            }
            else
            {
                List<ZC_SJZDList> list = new List<ZC_SJZDList>();
                foreach (DataRow row in rows)
                {
                    ZC_SJZDList zds = new ZC_SJZDList();
                    ZC_SJZD zd = zds.SysDictionary;
                    zd.Item = Convert.ToString(row["Item"]);
                    zd.Flag = Convert.ToString(row["Flag"]);
                    zd.FlagValue = Convert.ToString(row["FlagValue"]);
                    zd.ParentID = Convert.ToString(row["ParentID"]);
                    zd.IsUse = Convert.ToString(row["IsUse"]);
                    List<ZC_SJZDList> zdss = await CreateZDlist(table, zd.Flag);

                    if (zdss != null)
                    {
                        zds.SysDictionaryList = zdss;
                    }
                    list.Add(zds);

                }
                return list;
            }
        }


        public static async Task InsertZC_SJZD(ZC_SJZD zd)
        {
            await new RepositoryFactory().BaseRepository().Insert(zd);
        }

        public static async Task UpdateZC_SJZD(ZC_SJZD zd)
        {
            await new RepositoryFactory().BaseRepository().Update(zd);
        }

        public static async Task DeleteZC_SJZD(string item, string Flag)
        {
            string sql = "Update ZC_SJZD set IsUse='0' where Flag='" + Flag + "' and Item='" + item + "'";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        }
        #endregion

        #region "系统设置"

        public static async Task<DataTable> GetZC_JBSZ()
        {
            string sql = "select * from ZC_JBSZ";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }

        public static async Task InsertZC_JBSZ(ZC_JBSZ jb)
        {
            string sql = "delete from ZC_JBSZ;";
            sql += "insert into ZC_JBSZ values('" + jb.Ztxx + "','" + jb.Ztmc + "','" + jb.Kjzg + "','" + jb.Qyrq + "','" + jb.Zclbfj + "'," +
                "'" + jb.Cfddfj + "','" + jb.Zcdmzdy + "','" + jb.Zcdmbdc + "','" + jb.Zcdmdc + "','" + jb.Zcdmzdlr + "','" + jb.Qzdm + "','" + jb.Lscd + "');";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        }

        public static async Task<DataTable> GetZC_KPSZ()
        {
            string sql = "select * from ZC_KPSZ";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }

        public static async Task InsertZC_KPSZ(ZC_KPSZ jb)
        {
            string sql = "delete from ZC_KPSZ;";
            sql += "insert into ZC_KPSZ values('" + jb.Lbmc + "','" + jb.Zs + "','" + jb.Zx + "','" + jb.Yjsynxdw + "','" + jb.Tmdy + "'," +
                "'" + jb.Tmxs + "','" + jb.Dyfs + "','" + jb.Ybd + "','" + jb.Ylbd + "','" + jb.Nxbd + "','" + jb.Bdlx + "','" + jb.Pzx + "');";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        }

        public static async Task<DataTable> GetZC_ZJSZ()
        {
            string sql = "select * from ZC_ZJSZ";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }

        public static async Task InsertZC_ZJSZ(ZC_ZJSZ jb)
        {
            string sql = "delete from ZC_ZJSZ;";
            sql += "insert into ZC_ZJSZ values('" + jb.Xyjt + "','" + jb.Dyjt + "','" + jb.Qbtw + "','" + jb.Qbzx + "','" + jb.Zjljd + "'," +
                "'" + jb.Jczljd + "');";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        }

        public static async Task<DataTable> GetZC_JZSZ()
        {
            string sql = "select * from ZC_JZSS";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }

        public static async Task InsertZC_JZSS(ZC_JZSS jb)
        {
            string sql = "delete from ZC_JZSS;";
            sql += "insert into ZC_JZSS values('" + jb.Jzqwc + "','" + jb.Bhz + "','" + jb.Dzfs + "','" + jb.Bpyx + "','" + jb.Syjz + "');";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        }
        #endregion 

        #region "公用"
        public static async Task<String> CreateZCDM(string Dwdm, string Lx)
        {
            string zcdm = "";
            string strsql = "select isnull(max(convert(bigint, Zcdm)),0)+1 as zcdm from ZC_ZCBH where dwdm='" + Dwdm + "' and Lx='" + Lx + "'";
            DataTable dt = await new RepositoryFactory().BaseRepository().FindTable(strsql, null);
            long id = 0;
            if (dt.Rows.Count > 0)
            {
                id = Convert.ToInt64(dt.Rows[0]["zcdm"]);
            }
            for (int i = 6; i > id.ToString().Length; i--)
            {
                zcdm += "0";
            }
            zcdm = Dwdm + Lx + zcdm + id;
            strsql = "insert into ZC_ZCBH(Dwdm,Zcdm,Lx) values('" + Dwdm + "','" + id + "','" + Lx + "')";
            await new RepositoryFactory().BaseRepository().ExecuteSql(strsql);
            return zcdm;
        }

        public static async Task<String> CreateZCDM(IRepository irp, string Dwdm, string Lx)
        {
            string zcdm = "";
            string strsql = "select isnull(max(convert(bigint, Zcdm)),0)+1 as zcdm from ZC_ZCBH where dwdm='" + Dwdm + "' and Lx='" + Lx + "'";
            DataTable dt = await irp.FindTable(strsql, null);
            long id = 0;
            if (dt.Rows.Count > 0)
            {
                id = Convert.ToInt64(dt.Rows[0]["zcdm"]);
            }
            for (int i = 6; i > id.ToString().Length; i--)
            {
                zcdm += "0";
            }
            zcdm = Dwdm + Lx + zcdm + id;
            strsql = "insert into ZC_ZCBH(Dwdm,Zcdm,Lx) values('" + Dwdm + "','" + id + "','" + Lx + "')";
            await irp.ExecuteSql(strsql);
            return zcdm;
        }

        #endregion

        #region "资产预算"
        public static async Task<(IEnumerable<dynamic>, int)> GetZC_Ys(ZC_YSQuerry querry)
        {
            string str = " and 1=1 ";
            if (!string.IsNullOrEmpty(querry.Dwdm) && querry.Dwdm != "string")
            {
                str += "and Dwdm='" + querry.Dwdm + "'";
            }
            if (!string.IsNullOrEmpty(querry.Bmdm) && querry.Bmdm != "string")
            {
                str += " and bmdm='" + querry.Bmdm + "'";
            }
            if (!string.IsNullOrEmpty(querry.Zcmc) && querry.Zcmc != "string")
            {
                str += " and zcmc like '%" + querry.Zcmc + "%'";
            }
            if (!string.IsNullOrEmpty(querry.Zt) && querry.Zt != "string")
            {
                str += " and zt='" + querry.Zt + "'";
            }
            if (!string.IsNullOrEmpty(querry.Kjnd) && querry.Kjnd != "string")
            {
                str += " and Kjnd='" + querry.Kjnd + "'";
            }
            string sql = @"select ListID,Djbh,Kjnd,Dwdm,Dwmc,Bmdm,Bmmc,Syr,Cfdd,Zcdm,Zcmc,Zclbdm,Zclbmc,Ggxh,Jldw,Yszlkz,Ysdjkz,Yszjkz,Sfzy
                     ,Yzysl,Yzyed,Sysl,Syed,Cgfsdm,Cgfsmc,Yt,Bz,ZdrID,Zdr,Zdrq,ShrID,Shr,Shrq,(case when zt is null then '1' when zt='' then '1' else Zt end) as Zt,Ysdwdm,Ysdwmc,Ysbmdm,Ysbmmc,Ssbmdm],Ssbmmc] from ZC_YS where dwdm in " + querry.DepartID + str;
            bool isAsc = true;
            if (querry.Sort.ToUpper() != "ASC")
            {
                isAsc = false;
            }
            //return await new RepositoryFactory().BaseRepository().FindTable(sql, null, querry.SortString, isAsc, querry.Size, querry.Current);
            return await new RepositoryFactory().BaseRepository().FindList(sql, null, querry.SortString, isAsc, querry.Size, querry.Current);
        }

        public static async Task<DataTable> GetZCYS(string kjnd, string dwdm, string bmdm, string zcdm, decimal sl, decimal dj, decimal zj)
        {
            string sql = "select * from ZC_Ys where kjnd='" + kjnd + "' and dwdm='" + dwdm + "' and bmdm='" + bmdm + "' and Ysdjkz=" + dj + " and Yszlkz=" + sl + " and Yszjkz=" + zj + " and zcdm='" + zcdm + "'";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }
        public static async Task InsertZC_Ys(ZC_YS zc)
        {
            await new RepositoryFactory().BaseRepository().Insert(zc);

        }

        public static async Task UpdateZC_YS(ZC_YS zc)
        {
            await new RepositoryFactory().BaseRepository().Update(zc);
        }

        public static async Task DeleteZC_YS(ZC_YS zc)
        {
            await new RepositoryFactory().BaseRepository().Update(zc);
        }

        public static async Task<DataTable> GetZC_YSByID(string listid)
        {

            string sql = @"if(not exists(select * from ZC_YS where ListID='" + listid + @"'))
                        begin
                        select* from ZC_YS where Djbh = '" + listid + @"'
                        end
                        else
                        begin
                        select* from ZC_YS where ListID = '" + listid + @"'
                        end";

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);

        }

        #endregion

        #region "预算调整"
        public static async Task<(DataTable, int)> GetZC_YSTZ(ZC_YSTZQuerry querry)
        {
            string str = " and 1=1 ";
            if (!string.IsNullOrEmpty(querry.Dwdm) && querry.Dwdm != "string")
            {
                str += "and a.Dwdm='" + querry.Dwdm + "'";
            }
            if (!string.IsNullOrEmpty(querry.Bmdm) && querry.Bmdm != "string")
            {
                str += " and a.bmdm='" + querry.Bmdm + "'";
            }
            if (!string.IsNullOrEmpty(querry.Zcmc) && querry.Zcmc != "string")
            {
                str += " and a.zcmc like '%" + querry.Zcmc + "%'";
            }
            if (!string.IsNullOrEmpty(querry.Zt) && querry.Zt != "string")
            {
                str += " and a.zt='" + querry.Zt + "'";
            }
            if (querry.Sbrqs != null && querry.Sbrqe != null)
            {
                if (querry.Sbrqe > querry.Sbrqs)
                {
                    str += " and (a.sbrq>='" + querry.Sbrqs + "' and a.sbrq<='" + querry.Sbrqe + "')";
                }
            }
            string sql = @"select a.[ListID],a.[Djbh],a.[Ysid],a.[Kjnd],a.[Dwdm],a.[Dwmc],a.[Bmdm],a.[Bmmc],a.[Sqr] ,a.[Zcdm]
      ,a.[Zcmc],a.[Zclbdm],a.[Zclbmc],a.[Ggxh],a.[Jldw],a.[Yszlkz],a.[Ysdjkz],a.[Yszjkz],a.[Sbrq],[Sblx],a.[Bz]
      ,(case when a.[Zt] is null then '1' when a.Zt = '' then '1' else a.Zt end) as Zt,a.[ZdrID],a.[Zdr],a.[Zdrq],a.[ShrID],a.[Shr],a.[Shrq],
	  b.Yszjkz as Yszjkzq,b.Yszlkz as Yszlkzq,b.Ysdjkz as Ysdjkzq,b.Sfzy,b.Yzysl,b.Yzyed,b.Sysl,b.Syed from ZC_YSTZ a left join ZC_YS b on a.Ysid=b.Djbh where a.dwdm in " + querry.DepartID + str;
            bool isAsc = true;
            if (querry.Sort.ToUpper() != "ASC")
            {
                isAsc = false;
            }
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null, querry.SortString, isAsc, querry.Size, querry.Current);

        }

        public static async Task InsertZC_YSTZ(ZC_YSTZ zc)
        {
            await new RepositoryFactory().BaseRepository().Insert(zc);

        }

        public static async Task UpdateZC_YSTZ(ZC_YSTZ zc)
        {
            await new RepositoryFactory().BaseRepository().Update(zc);
            if (zc.Zt == "2")
            {
                string sql = "update ZC_YS set zcdm='" + zc.Zcdm + "',zcmc='" + zc.Zcmc + "',zclbdm='" + zc.Zclbdm + "',zclbmc='" + zc.Zclbmc + "',ggxh='" + zc.Ggxh + "',jldw='" + zc.Jldw + "'" +
                    ",Yszlkz=" + zc.Yszlkz + ",Ysdjkz=" + zc.Ysdjkz + ",Yszjkz=" + zc.Yszjkz + " where Djbh='" + zc.Ysid + "'";
                await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
            }
        }

        public static async Task DeleteZC_YSTZ(ZC_YSTZ zc)
        {
            string sql = "update ZC_YSTZ set Zt='0' where Listid='" + zc.ListID + "'";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
            // await new RepositoryFactory().BaseRepository().Update(zc);
            //string sql = "delete from ZC_YSTZ where TzID='" + zc.ListID;
            //await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        }

        public static async Task<DataTable> GetZC_YSTZByID(string listid)
        {

            string sql = @"select a.[ListID],a.[Djbh],a.[Ysid],a.[Kjnd],a.[Dwdm],a.[Dwmc],a.[Bmdm],a.[Bmmc],a.[Sqr] ,a.[Zcdm]
      , a.[Zcmc],a.[Zclbdm],a.[Zclbmc],a.[Ggxh],a.[Jldw],a.[Yszlkz],a.[Ysdjkz],a.[Yszjkz],a.[Sbrq],[Sblx],a.[Bz]
      ,(case when a.[Zt] is null then '1' when a.Zt = '' then '1' else a.Zt end) as Zt,a.[ZdrID],a.[Zdr],a.[Zdrq],a.[ShrID],a.[Shr],a.[Shrq],
	  b.Yszjkz as Yszjkzq,b.Yszlkz as Yszlkzq,b.Ysdjkz as Ysdjkzq,b.Sfzy,b.Yzysl,b.Yzyed,b.Sysl,b.Syed,b.Dwdm as ysssdwdm,b.Dwmc as ysssdwmc,b.Bmdm as ysbmdm,
	  b.Bmmc as ysbmmc,b.Bmdm as zcbmdm,b.Bmmc as zcbmmc from ZC_YSTZ a left join ZC_YS b on a.Ysid = b.Djbh where a.ListID='" + listid + "'";

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);

        }

        public static async Task<DataTable> GetZC_YSTZMX(string listid)
        {

            string sql = "select * from ZC_YSTZNR where tzID='" + listid + "'";

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);

        }
        public static async Task InsertZC_YSTZMX(ZC_YSTZNR zc)
        {
            await new RepositoryFactory().BaseRepository().Insert(zc);

        }

        public static async Task UpdateZC_YSTZMX(ZC_YSTZNR zc)
        {
            await new RepositoryFactory().BaseRepository().Update(zc);
        }

        public static async Task DeleteZC_YSTZMX(ZC_YSTZNR zc)
        {

            await new RepositoryFactory().BaseRepository().Delete(zc);
        }

        #endregion

        #region "采购目录"

        public static async Task<DataTable> GetZC_CGMLAll()
        {
            string sql = "select Mldm,Mlmc,Parentdm from ZC_CGML order by Mldm;";

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);

        }

        public static async Task<List<CGMLTree>> GetZC_CGMLAllTree(string zclb)
        {
            string sql = "select Mldm,Mlmc,Parentdm from ZC_CGML order by Mldm;";
            if (zclb != "" & zclb != null)
            {
                sql = @"select Mldm,Mlmc,Parentdm from ZC_CGML where Mldm like '%" + zclb + "%' or Mlmc like '%" + zclb + @"%' or
            Mldm in (select Parentdm from ZC_CGML where Mldm like '%" + zclb + "%' or Mlmc like '%" + zclb + @"%' or
            Mldm in (select Parentdm from ZC_CGML where Mldm like '%" + zclb + "%' or Mlmc like '%" + zclb + @"%' or
            Mldm in (select Parentdm from ZC_CGML where Mldm like '%" + zclb + "%' or Mlmc like '%" + zclb + @"%'or
            Mldm in (select Parentdm from ZC_CGML where Mldm like '%" + zclb + "%' or Mlmc like '%" + zclb + @"%'))))";
            }
            DataTable table = await new RepositoryFactory().BaseRepository().FindTable(sql, null);
            List<CGMLTree> list = new List<CGMLTree>();
            foreach (DataRow row in table.Select("Parentdm=''"))
            {
                CGMLTree cgml = new CGMLTree();
                cgml.Mldm = Convert.ToString(row["mldm"]);
                cgml.Mlmc = Convert.ToString(row["mlmc"]);
                List<CGMLTree> slist = await CreateTree(table, cgml.Mldm);
                if (slist != null)
                {
                    cgml.Child = slist;
                }
                list.Add(cgml);
            }
            return list;
        }

        public static async Task<List<CGMLTree>> GetZC_CGMLAllTreeByZCDL(string zcdl)
        {
            string sql = "select Mldm,Mlmc,Parentdm from ZC_CGML where zcdl='" + zcdl + "' order by Mldm;";

            DataTable table = await new RepositoryFactory().BaseRepository().FindTable(sql, null);
            List<CGMLTree> list = new List<CGMLTree>();
            foreach (DataRow row in table.Select("Parentdm=''"))
            {
                CGMLTree cgml = new CGMLTree();
                cgml.Mldm = Convert.ToString(row["mldm"]);
                cgml.Mlmc = Convert.ToString(row["mlmc"]);
                List<CGMLTree> slist = await CreateTree(table, cgml.Mldm);
                if (slist != null)
                {
                    cgml.Child = slist;
                }
                list.Add(cgml);
            }
            return list;
        }

        //public static async Task<List<CGMLTree>> Querry(List<CGMLTree> list, string zclb)
        //{
        //    List<CGMLTree> slist = list;
        //    foreach (CGMLTree tree in list)
        //    {
        //        if (!tree.Mldm.Contains(zclb) & !tree.Mlmc.Contains(zclb))
        //        {
        //            foreach (CGMLTree t in tree.Child)
        //            {
        //                if (!t.Mldm.Contains(zclb) & !t.Mlmc.Contains(zclb))
        //                {
        //                    foreach (CGMLTree t1 in t.Child)
        //                    {
        //                        if (!t1.Mldm.Contains(zclb) & !t1.Mlmc.Contains(zclb))
        //                        {
        //                            foreach (CGMLTree t2 in t1.Child)
        //                            {
        //                                if (!t2.Mldm.Contains(zclb) & !t2.Mlmc.Contains(zclb))
        //                                {
        //                                    foreach (CGMLTree t3 in t2.Child)
        //                                    {
        //                                        if (!t3.Mldm.Contains(zclb) & !t3.Mlmc.Contains(zclb))
        //                                        {
        //                                            slist.Remove(t3);
        //                                        }
        //                                    }
        //                                }
        //                            }
        //                        }
        //                    }
        //                }
        //            }
        //        }

        //    }
        //    return list;
        //}

        public static async Task<List<CGMLTree>> CreateTree(DataTable table, string mldl)
        {
            DataRow[] rows = table.Select("Parentdm='" + mldl + "'");
            if (rows.Length == 0)
            {
                return null;
            }
            else
            {
                List<CGMLTree> list = new List<CGMLTree>();

                foreach (DataRow row in rows)
                {
                    CGMLTree cgml = new CGMLTree();
                    cgml.Mldm = Convert.ToString(row["mldm"]);
                    cgml.Mlmc = Convert.ToString(row["mlmc"]);
                    List<CGMLTree> slist = await CreateTree(table, cgml.Mldm);
                    if (slist != null)
                    {
                        cgml.Child = slist;
                    }
                    list.Add(cgml);
                }
                return list;
            }
        }
        public static async Task<DataTable> GetZC_CGML(string Dldm)
        {
            string sql = "";
            if (Dldm != "" & Dldm != null)
            {
                sql = "select * from ZC_CGML where Dldm='" + Dldm + "'";
            }
            else
            {
                sql = "select * from ZC_CGML";
            }

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);

        }

        public static async Task<List<CGMLTree>> GetZC_CGMLByDLDM(string Dldm)
        {
            string sql = "";
            if (Dldm != "" & Dldm != null)
            {
                sql = "select * from ZC_CGML where Dldm='" + Dldm + "'";
            }
            else
            {
                sql = "select * from ZC_CGML";
            }
            DataTable table = await new RepositoryFactory().BaseRepository().FindTable(sql, null);
            List<CGMLTree> list = new List<CGMLTree>();
            foreach (DataRow row in table.Select("Parentdm='10'"))
            {
                CGMLTree cgml = new CGMLTree();
                cgml.Mldm = Convert.ToString(row["mldm"]);
                cgml.Mlmc = Convert.ToString(row["mlmc"]);
                List<CGMLTree> slist = await CreateTree(table, cgml.Mldm);
                if (slist != null)
                {
                    cgml.Child = slist;
                }
                list.Add(cgml);
            }
            return list;

        }



        public static async Task<DataTable> GetZC_ZCDL()
        {
            string sql = "select * from ZC_CGML where Parentdm='10' order by Mldm;";

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);

        }

        public static async Task<DataTable> GetZC_CGMLQuerry(string Mlmc, string Mldm, string Zt)
        {
            string str = " 1=1 ";
            if (Mlmc != "")
            {
                str += " and Mlmc like '" + Mlmc + "%'";
            }
            if (Mldm != "")
            {
                str += " and Mldm like '" + Mldm + "%'";
            }
            if (Zt != "")
            {
                str += " and Zt = '" + Zt + "'";
            }
            string sql = "select * from ZC_CGML where " + str + " order by Mldm";

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);

        }

        public static async Task<(DataTable, int)> GetZC_CGMLByParent(string Parentdm, string cgml, int Current, int Size)
        {
            string sql = "select * from ZC_CGML where 1=1 ";
            if (Parentdm != "" & Parentdm != "0" & Parentdm != null)
            {
                sql = "select * from ZC_CGML where mldm like '" + Parentdm + "%'";
            }
            if (cgml != "" & cgml != null)
            {
                sql += " and (Mldm like '%" + cgml + "%' or Mlmc like '%" + cgml + "%')";
            }

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null, "mldm", true, Size, Current);

        }

        public static async Task InsertZC_CGML(ZC_CGML cgml)
        {
            await new RepositoryFactory().BaseRepository().Insert(cgml);

        }
        public static async Task UpdateZC_CGML(ZC_CGML zc)
        {
            await new RepositoryFactory().BaseRepository().Update(zc);
        }

        public static async Task DeleteZC_CGML(string ListID)
        {
            string sql = "Update ZC_CGML set Zt='0' where Mldm='" + ListID + "'";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        }
        #endregion

        #region "资产配置"
        public static async Task<(DataTable, int)> GetZC_ZCPZ(ZC_ZCPZQuerry querry)
        {
            string str = " and 1=1 ";
            if (!string.IsNullOrEmpty(querry.Dwdm) && querry.Dwdm != "string")
            {
                str += "and Dwdm='" + querry.Dwdm + "'";
            }
            if (!string.IsNullOrEmpty(querry.Bmdm) && querry.Bmdm != "string")
            {
                str += " and bmdm='" + querry.Bmdm + "'";
            }
            if (!string.IsNullOrEmpty(querry.Zcmc) && querry.Zcmc != "string")
            {
                str += " and zcmc like '%" + querry.Zcmc + "%'";
            }
            if (!string.IsNullOrEmpty(querry.Zt) && querry.Zt != "string")
            {
                str += " and zt='" + querry.Zt + "'";
            }
            if (!string.IsNullOrEmpty(querry.Zrr) && querry.Zt != "string")
            {
                str += " and Zrr like '%" + querry.Zrr + "%'";
            }
            if (querry.Sbrqs != null && querry.Sbrqe != null)
            {
                if (querry.Sbrqe > querry.Sbrqs)
                {
                    str += " and (sbrq>='" + querry.Sbrqs + "' and sbrq<='" + querry.Sbrqe + "')";
                }
            }
            string sql = "select * from ZC_ZCPZ where dwdm in " + querry.DepartID + str;
            bool isAsc = true;
            if (querry.Sort.ToUpper() != "ASC")
            {
                isAsc = false;
            }
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null, querry.SortString, isAsc, querry.Size, querry.Current);

        }




        public static async Task<string> InsertZC_ZCPZ(ZC_ZCPZ zc)
        {
            await new RepositoryFactory().BaseRepository().Insert(zc);
            return zc.ListID;

        }

        public static async Task UpdateZC_ZCPZ(ZC_ZCPZ zc)
        {
            await new RepositoryFactory().BaseRepository().Update(zc);
        }

        public static async Task DeleteZC_ZCPZ(string listid)
        {
            string sql = "update ZC_ZCPZ set zt='0' where ListID='" + listid + "'";
            // await new RepositoryFactory().BaseRepository().Update(zc);
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        }

        public static async Task<DataTable> GetZC_ZCPZByID(string listid)
        {

            string sql = "select * from ZC_ZCPZ where ListID='" + listid + "'";

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);

        }

        public static async Task<DataTable> GetZC_ZCPZByYsID(string listid)
        {

            string sql = "select a.*,b.Kjnd,c.Dwdm,c.Dwmc,c.Sbrq from ZC_ZCPZMX a left join ZC_YS b on a.YsID=b.Djbh left join ZC_ZCPZ c on a.PzID=c.ListID where a.Ysid='" + listid + "'";

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);

        }

        public static async Task<DataTable> GetZC_PZYYYS(string Pzid)
        {
            string sql = "select a.*,b.ListID as pzmxid from ZC_YS a left join ZC_ZCPZMX b on a.listid=b.YsID where PzID='" + Pzid + "'";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }

        public static async Task<DataTable> GetZC_ZCPZMX(string listid)
        {

            string sql = "select * from ZC_ZCPZMX where PzID='" + listid + "'";

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);

        }

        public static async Task<DataTable> GetZC_ZCPZMXByID(string listid)
        {

            string sql = "select * from ZC_ZCPZMX where ListID='" + listid + "'";

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);

        }
        public static async Task InsertZC_ZCPZMX(ZC_ZCPZMX zc)
        {
            await new RepositoryFactory().BaseRepository().Insert(zc);
            string sql = "update ZC_ZCPZ set Zcsgze=Zcsgze+" + zc.Sgzj + " where ListID='" + zc.PzID + "';";
            sql += "update ZC_ZCPZ set Zcysze =(select sum(Yszjkz) from ZC_YS where listid in (select ysid from ZC_ZCPZMX where pzid='" + zc.PzID + "')) where ListID = '" + zc.PzID + "';";
        }

        public static async Task InsertZC_PZMX(string[] str, string pzid)
        {
            int index = 0;
            foreach (string s in str)
            {
                string sql = "insert into ZC_ZCPZMX select '" + DateTime.Now.ToString("yyyyMMddHHmmssffffff") + index + "'," +
                "'" + pzid + "',djbh,Zclbdm,Zclbmc,Zcdm,Zcmc,Ggxh,Jldw,Ysdjkz,Yszlkz,Yszjkz,Cgfsdm,Cgfsmc,Yt,Bz,'1',Cfdd,Syr,Ssbmdm,Ssbmmc from ZC_YS where djbh='" + s + "';";

                await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
                index++;
            }

            //string id = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");

        }

        public static async Task UpdateZC_ZCPZMX(ZC_ZCPZMX zc)
        {
            await new RepositoryFactory().BaseRepository().Update(zc);
            string sql = "update ZC_YS set Sfzy='是',Yzysl=Yzysl+" + zc.Sgsl + ",Yzyed=Yzyed+" + zc.Sgzj + " where djbh='" + zc.YsID + "';";
            //await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
            sql += "declare @ysze decimal(20, 2);select @ysze=Yszjkz from ZC_YS where djbh = '" + zc.YsID + "'; ";
            sql += "update ZC_YS set Sysl=Yszlkz-Yzysl,Syed=Yszjkz-Yzyed where  ListID='" + zc.YsID + "';";
            sql += "update ZC_ZCPZ set Zcsgze=isnull(Zcsgze,0)+" + zc.Sgzj + ",Zcysze=isnull(Zcysze,0)+@ysze where ListID='" + zc.PzID + "';";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        }

        public static async Task DeleteZC_ZCPZMXBYYSID(string ysid)
        {
            //await new RepositoryFactory().BaseRepository().Delete(zc);
            string sql = "delete from ZC_ZCPZMX where ysid='" + ysid + @"';";
            sql += "declare @sgsl decimal(20,2);declare @sgze decimal(20,2);";
            sql += "update ZC_YS set Sfzy='是',Yzysl=Yzysl-@sgsl,Yzyed=Yzyed+@sgze where djbh='" + ysid + "';";
            //await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
            sql += "declare @ysze decimal(20, 2);select @ysze=Yszjkz from ZC_YS where djbh = '" + ysid + "'; ";
            sql += "update ZC_YS set Sysl=Yszlkz-Yzysl,Syed=Yszjkz-Yzyed where  ListID='" + ysid + "';";
            //sql += "update ZC_ZCPZ set Zcsgze=isnull(Zcsgze,0)+" + zc.Sgze + ",Zcysze=isnull(Zcysze,0)+@ysze where ListID='" + zc.PzID + "';";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);

        }

        public static async Task DeleteZC_ZCPZMX(ZC_ZCPZMX zc)
        {
            //await new RepositoryFactory().BaseRepository().Delete(zc);
            string sql = "delete from ZC_ZCPZMX where listid='" + zc.ListID + @"';";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
            sql = "update ZC_YS set Sysl=Yszlkz+Yzysl,Syed=Yszjkz+Yzyed where  ListID='" + zc.YsID + "';";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        }

        public static async Task DeleteZC_ZCPZMXBYID(string listid)
        {
            //await new RepositoryFactory().BaseRepository().Delete(zc);
            string sql = "declare @sgsl decimal(20,2);declare @sgze decimal(20,2);declare @ysbh varchar(50);";
            sql += "select @sgsl=Sgsl,@sgze=Sgze,@ysbh=ysid from ZC_ZCPZMX where ListID='" + listid + "';";
            sql += "delete from ZC_ZCPZMX where listid='" + listid + @"';
                update ZC_YS set Sfzy='是',Yzysl=Yzysl-@sgsl,Yzyed=Yzyed-@sgze where djbh=@ysbh;";
            sql += "declare @ysze decimal(20, 2);select @ysze=Yszjkz from ZC_YS where djbh =@ysbh; ";
            sql += "update ZC_YS set Sysl=Yszlkz-Yzysl,Syed=Yszjkz-Yzyed where djbh =@ysbh;;";
            sql += "update ZC_ZCPZ set Zcsgze=isnull(Zcsgze,0)-@sgze,Zcysze=isnull(Zcysze,0)-@ysze where ListID='" + listid + "';";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);

        }

        public static async Task CreateCard(string listid)
        {
            string sql = "";
            sql = "Insert into ZC_ZCKP (Zcdm,Zcmc,Dwdm,Dwmc,Bmdm,bmmc,Zclbdm,Zclbmc,Ggxh,Sl,Yz,Jldw,Gzsj,Sqdh)" +
                @"select b.Zcdm,b.Zcmc,a.Dwdm,a.Dwmc,b.Bmdm,b.Bmmc,b.Zclbdm,b.Zclbmc,b.Ggxh,b.Sgsl,b.Sgze,b.Jldw,CONVERT(char(8), a.Sbrq,112) as sbsj,a.ListID
                from ZC_ZCPZ a left
                join ZC_ZCPZMX b on a.ListID = b.PzID where a.ListID=" + listid;
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql, null);
        }

        public static async Task<(DataTable, int)> GetWrkzc(ZC_ZCPZQuerry querry)
        {
            string str = " and 1=1 ";
            if (!string.IsNullOrEmpty(querry.Dwdm) && querry.Dwdm != "string")
            {
                str += "and Dwdm='" + querry.Dwdm + "'";
            }
            if (!string.IsNullOrEmpty(querry.Bmdm) && querry.Bmdm != "string")
            {
                str += " and bmdm='" + querry.Bmdm + "'";
            }
            if (!string.IsNullOrEmpty(querry.Zcmc) && querry.Zcmc != "string")
            {
                str += " and zcmc like '%" + querry.Zcmc + "%'";
            }
            if (querry.Sbrqs != null && querry.Sbrqe != null)
            {
                if (querry.Sbrqe > querry.Sbrqs)
                {
                    str += " and (sbrq>='" + querry.Sbrqs + "' and sbrq<='" + querry.Sbrqe + "')";
                }
            }
            string sql = @"select a.*,b.Bmdm,b.Bmmc,b.Dwdm,b.Dwmc,b.Sbrq as gzrq,LEFT(Zclbdm,1) as Zcdl,b.Zcsgze,b.Zcysze,c.Yjsynx,c.Yjsyyf from ZC_ZCPZMX a left join ZC_ZCPZ b 
                            on a.PzID = b.ListID left join ZC_CGML c on a.Zclbdm = c.Mldm where (isnull(a.zt,'0')='0' or a.Zt='') and b.zt='4' and dwdm in " +
                            querry.DepartID + str;
            bool isAsc = true;
            if (querry.Sort.ToUpper() != "ASC")
            {
                isAsc = false;
            }
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null, querry.SortString, isAsc, querry.Size, querry.Current);
        }


        public static async Task<DataTable> GetWrkzcByID(string Listid)
        {
            string sql = @"select a.*,b.Bmdm,b.Bmmc,b.Dwdm,b.Dwmc,b.Sbrq as gzrq,LEFT(Zclbdm, 1) as Zcdl,b.Zcsgze,b.Zcysze,c.Yjsynx,c.Yjsyyf from ZC_ZCPZMX a left join ZC_ZCPZ b
                            on a.PzID = b.ListID left join ZC_CGML c on a.Zclbdm = c.Mldm where(isnull(a.zt, '0') = '0' or a.Zt = '') and b.zt = '4' and a.ListID='" + Listid + "'";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }
        #endregion

        #region "资产卡片"
        public static async Task<(DataTable, int)> GetZC_ZCKP(ZC_ZCKPQuerry querry)
        {
            string str = " and 1=1 ";
            if (!string.IsNullOrEmpty(querry.Dwdm) && querry.Dwdm != "string")
            {
                str += "and Dwdm='" + querry.Dwdm + "'";
            }
            if (!string.IsNullOrEmpty(querry.Bmdm) && querry.Bmdm != "string")
            {
                str += " and glbmdm='" + querry.Bmdm + "'";
            }
            if (!string.IsNullOrEmpty(querry.Zcmc) && querry.Zcmc != "string")
            {
                str += " and zcmc like '%" + querry.Zcmc + "%'";
            }


            string sql = "select * from ZC_ZCKP where dwdm in " + querry.DepartID + str;
            bool isAsc = true;
            if (querry.Sort.ToUpper() != "ASC")
            {
                isAsc = false;
            }
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null, querry.SortString, isAsc, querry.Size, querry.Current);

        }

        public static async Task InsertZC_ZCKP(ZC_ZCKP zc)
        {
            await new RepositoryFactory().BaseRepository().Insert(zc);
            if (zc.Sfjyxzc == "是")
            {
                ZC_JYXZC jy = new ZC_JYXZC();
                jy.Zcdm = zc.Zcdm;

                jy.Dwdm = zc.Dwdm;
                jy.Dwmc = zc.Dwmc;
                jy.Bmdm = zc.Glbmdm;
                jy.Bmmc = zc.Glbmmc;
                jy.Gldwdm = zc.Dwdm;
                jy.Gldwmc = zc.Glbmmc;
                jy.Jzmj = zc.Syqmj;
                jy.Sjkymj = zc.Syqmj;
                await new RepositoryFactory().BaseRepository().Insert(jy);
            }
        }

        public static async Task UpdateZC_ZCKP(ZC_ZCKP zc)
        {
            await new RepositoryFactory().BaseRepository().Update(zc);
        }

        public static async Task DeleteZC_ZCKP(ZC_ZCKP zc)
        {
            await new RepositoryFactory().BaseRepository().Update(zc);
        }

        public static async Task<DataTable> GetZC_ZCKPByID(string listid)
        {

            string sql = "select * from ZC_ZCKP where ListID='" + listid + "'";

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);

        }

        #endregion

        #region "资产卡片设施"
        public static async Task<DataTable> GetZC_KPSS(string zcdm)
        {

            string sql = "select * from ZC_KPSS where Zcdm ='" + zcdm + "'";

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);

        }

        public static async Task InsertZC_KPSS(ZC_KPSS zc)
        {
            await new RepositoryFactory().BaseRepository().Insert(zc);

        }

        public static async Task UpdateZC_KPSS(ZC_KPSS zc)
        {
            await new RepositoryFactory().BaseRepository().Update(zc);
        }

        public static async Task DeleteZC_KPSS(string listid)
        {
            string sql = "Update ZC_KPSS set Zt='2' where ListID='" + listid + "'";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        }

        public static async Task<DataTable> GetZC_KPSSByID(string listid)
        {

            string sql = "select * from ZC_KPSS where ListID='" + listid + "'";

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);

        }

        #endregion

        #region "土地资产入库"

        public static async Task<(DataTable, int)> GetZCRKALL(ZC_RKQuerry zc)
        {
            string str = "and 1=1 ";
            if (!string.IsNullOrEmpty(zc.Zcmc) && zc.Zcmc != "string")
            {
                str += "and zcmc like'" + zc.Zcmc + "'";
            }

            if (!string.IsNullOrEmpty(zc.Zclbdm) && zc.Zclbdm != "string")
            {
                str += "and Zclbdm ='" + zc.Zclbdm + "'";
            }

            if (!string.IsNullOrEmpty(zc.Zt) && zc.Zt != "string")
            {
                str += "and zt ='" + zc.Zt + "'";
            }
            string sql = @"select * from (
                        select a.ListID,a.Pzdh,a.Zcdm,a.Zcmc,b.Zclbdm,b.Zclbmc,Rksj,b.Ggxh,b.Jldw,a.Zt,'1' as zcdl,a.Dwdm,Rksl,Rkdj,Rkzj from ZC_ZCRKJ a left 
                        join ZC_ZCPZMX b on a.Pzdh = b.ListID
                        union
                        select a.ListID,a.Pzdh,a.Zcdm,a.Zcmc,b.Zclbdm,b.Zclbmc,Rksj,b.Ggxh,b.Jldw,a.Zt,'2' as zcdl,a.Dwdm,Rksl,Rkdj,Rkzj from ZC_ZCRKS a left 
                        join ZC_ZCPZMX b on a.Pzdh = b.ListID
                        union
                        select a.ListID,a.Pzdh,a.Zcdm,a.Zcmc,b.Zclbdm,b.Zclbmc,Rksj,b.Ggxh,b.Jldw,a.Zt,'3' as zcdl,a.Dwdm,Rksl,Rkdj,Rkzj from ZC_ZCRKZ a left 
                        join ZC_ZCPZMX b on a.Pzdh = b.ListID
                        union
                        select a.ListID,a.Pzdh,a.Zcdm,a.Zcmc,b.Zclbdm,b.Zclbmc,Rksj,b.Ggxh,b.Jldw,a.Zt,'4' as zcdl,a.Dwdm,Rksl,Rkdj,Rkzj from ZC_ZCRKW a left 
                        join ZC_ZCPZMX b on a.Pzdh = b.ListID
                        union
                        select a.ListID,a.Pzdh,a.Zcdm,a.Zcmc,b.Zclbdm,b.Zclbmc,Rksj,b.Ggxh,b.Jldw,a.Zt,'5' as zcdl,a.Dwdm,Rksl,Rkdj,Rkzj from ZC_ZCRKD a left 
                        join ZC_ZCPZMX b on a.Pzdh = b.ListID
                        union
                        select a.ListID,a.Pzdh,a.Zcdm,a.Zcmc,b.Zclbdm,b.Zclbmc,Rksj,b.Ggxh,b.Jldw,a.Zt,'6' as zcdl,a.Dwdm,Rksl,Rkdj,Rkzj from ZC_ZCRKY a left join ZC_ZCPZMX b on a.Pzdh = b.ListID) as c";
            sql += " where Dwdm in " + zc.DepartID + " and zt!='0'" + str;
            bool isAsc = true;
            if (zc.Sort.ToUpper() != "ASC")
            {
                isAsc = false;
            }
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null, zc.SortString, isAsc, zc.Size, zc.Current);
        }

        public static async Task UpdateZT(string listid, string dldm, string zt)
        {
            string sql = "update ";
            switch (dldm)
            {
                case "1":
                    sql += "ZC_ZCRKJ";
                    break;
                case "2":
                    sql += "ZC_ZCRKS";
                    break;
                case "3":
                    sql += "ZC_ZCRKZ";
                    break;
                case "4":
                    sql += "ZC_ZCRKW";
                    break;
                case "5":
                    sql += "ZC_ZCRKD";
                    break;
                case "6":
                    sql += "ZC_ZCRKY";
                    break;
            }
            sql += " set zt='" + zt + "' where ListID='" + listid + "'";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        }
        public static async Task<(DataTable, int)> GetZC_ZCRKJ(ZC_ZCRKQuerry querry)
        {
            string str = " and 1=1 ";
            if (!string.IsNullOrEmpty(querry.Dwdm))
            {
                str += "and Dwdm='" + querry.Dwdm + "'";
            }
            if (!string.IsNullOrEmpty(querry.Bmdm))
            {
                str += " and bmdm='" + querry.Bmdm + "'";
            }
            if (!string.IsNullOrEmpty(querry.Zcmc))
            {
                str += " and zcmc like '%" + querry.Zcmc + "'%";
            }
            if (!string.IsNullOrEmpty(querry.Zt))
            {
                str += " and zt='" + querry.Zt + "'";
            }
            if (querry.Sbrqs != null && querry.Sbrqe != null)
            {
                if (querry.Sbrqe > querry.Sbrqs)
                {
                    str += " and (sbrq>='" + querry.Sbrqs + "' and sbrq<='" + querry.Sbrqe + "')";
                }
            }
            string sql = "select * from ZC_ZCRKJ where dwdm in " + querry.DepartID + str;
            bool isAsc = true;
            if (querry.Sort.ToUpper() != "ASC")
            {
                isAsc = false;
            }
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null, querry.SortString, isAsc, querry.Size, querry.Current);

        }

        public static async Task InsertZC_ZCRKJ(ZC_ZCRKJ zc)
        {
            await new RepositoryFactory().BaseRepository().Insert(zc);
            string sql = "Update ZC_ZCPZMX set zt='1' where ListID='" + zc.Pzdh + "'";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);

        }

        public static async Task UpdateZC_ZCRKJ(ZC_ZCRKJ zc)
        {
            await new RepositoryFactory().BaseRepository().Update(zc);
            //将入库资产写入资产卡片
            if (zc.Zt == "2")
            {
                string sql = @"declare @c int;
                set @c=(select count(1) from ZC_ZCKP where zcdm='" + zc.Zcdm + @"');
                if @c=0 begin Update ZC_ZCKP set rksl=rksl+" + zc.Rksj + ",rkzj=rkzj+" + zc.Rkzj + ",rkdj=" + zc.Rkdj + @" end
                else 
                INSERT INTO ZC_ZCKP
           (ListID, Dwdm, Dwmc, Glbmdm, Glbmmc, Zcdm, Zcmc, Rksl, Rkdj, Rkzj, Skr, Rksj, Qdfsdm, Qdfsmc
      , Qdrq, Gzrq, Zcdqr, Trsyrq, Ysxmbh, Syqmj, Dymj, Ftmj, Cqxsdm, Cqxsmc, Qszm, Qsxzdm, Qsxzmc, Qszh, Qsnx
      , Qsrdm, Qsrmc, Syqlxdm, Syqlxmc, Yt, Tdjcdm, Tdjcmc, Zlwz, Cfdd, Sfjyxzc, Bz, Zt, ZdrID, Zdr, Zdrq
      , Syqmj, Dymj)

      select ListID, Dwdm, Dwmc, bmdm, bmmc, Zcdm, Zcmc, Rksl, Rkdj, Rkzj, Skr, Rksj, Qdfsdm, Qdfsmc
      , Qdrq, Gzrq, Zcdqr, Trsyrq, Ysxmbh, Syqmj, Dymj, Ftmj, Cqxsdm, Cqxsmc, Qszm, Qsxzdm, Qsxzdmc, Qszh, Qsnx
      , Qsrdm, Qsrmc, Syqlxdm, Syqlxmc, Yt, Tdjcdm, Tdjcmc, Zlwz, Cfdd, Sfjyzc, Bz, '1', ZdrID, Zdr, Zdrq
      , Zymj, Zyzj from ZC_ZCRKJ where ListID='" + zc.ListID + "' end;";
                await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
                if (zc.Sfjyxzc == "是")
                {
                    ZC_JYXZC jy = new ZC_JYXZC();
                    jy.Zcdm = zc.Zcdm;

                    jy.Dwdm = zc.Dwdm;
                    jy.Dwmc = zc.Dwmc;
                    jy.Bmdm = zc.glbm;
                    jy.Bmmc = zc.glbm;
                    jy.Gldwdm = zc.Dwdm;
                    jy.Gldwmc = zc.Dwmc;
                    jy.Jzmj = zc.Syqmj;
                    jy.Sjkymj = zc.Syqmj;
                    await new RepositoryFactory().BaseRepository().Insert(jy);
                }
            }
        }

        public static async Task DeleteZC_ZCRKJ(string listid)
        {
            string sql = "Update ZC_ZCRKJ set zt='0' where listid='" + listid + "'";
            sql += "declare @zcdm varchar(50);";
            sql += "set @zcdm=(select zcdm from ZC_ZCRKJ where listid='" + listid + "');";
            sql += "Update ZC_ZCPZMX set zt='0' where zcdm=@zcdm;";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
            //await new RepositoryFactory().BaseRepository().Update(zc);

        }

        public static async Task<DataTable> GetZC_ZCRKJByID(string listid)
        {

            string sql = "select a.*,b.Sgdj,b.Sgsl,b.Sgzj,b.Zclbdm,b.Zclbmc from ZC_ZCRKJ a left join ZC_ZCPZMX b on  a.Pzdh=b.ListID where a.ListID='" + listid + "'";

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);

        }

        public static async Task<DataTable> GetZC_ZCRKMX(string listid)
        {

            string sql = "select * from ZC_ZCRKMX where PzID='" + listid + "'";

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);

        }
        public static async Task InsertZC_ZCRKMX(ZC_ZCRKMX zc)
        {
            await new RepositoryFactory().BaseRepository().Insert(zc);
            string sql = "Update ZC_ZCPZMX set zt='1' where zcdm='" + zc.Zcdm + "'";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);

        }

        public static async Task UpdateZC_ZCRKMX(ZC_ZCRKMX zc)
        {
            await new RepositoryFactory().BaseRepository().Update(zc);
        }

        public static async Task DeleteZC_ZCRKMX(ZC_ZCRKMX zc)
        {
            await new RepositoryFactory().BaseRepository().Delete(zc);
            string sql = "Update ZC_ZCPZMX set zt='0' where zcdm='" + zc.Zcdm + "'";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        }


        #endregion

        #region "资产通用设备入库"
        public static async Task<(DataTable, int)> GetZC_ZCRKS(ZC_ZCRKQuerry querry)
        {
            string str = " and 1=1 ";
            if (!string.IsNullOrEmpty(querry.Dwdm))
            {
                str += "and Dwdm='" + querry.Dwdm + "'";
            }
            if (!string.IsNullOrEmpty(querry.Bmdm))
            {
                str += " and bmdm='" + querry.Bmdm + "'";
            }
            if (!string.IsNullOrEmpty(querry.Zcmc))
            {
                str += " and zcmc like '%" + querry.Zcmc + "'%";
            }
            if (!string.IsNullOrEmpty(querry.Zt))
            {
                str += " and zt='" + querry.Zt + "'";
            }
            if (querry.Sbrqs != null && querry.Sbrqe != null)
            {
                if (querry.Sbrqe > querry.Sbrqs)
                {
                    str += " and (sbrq>='" + querry.Sbrqs + "' and sbrq<='" + querry.Sbrqe + "')";
                }
            }
            string sql = "select * from ZC_ZCRKS where dwdm in " + querry.DepartID + str;
            bool isAsc = true;
            if (querry.Sort.ToUpper() != "ASC")
            {
                isAsc = false;
            }
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null, querry.SortString, isAsc, querry.Size, querry.Current);

        }

        public static async Task InsertZC_ZCRKS(ZC_ZCRKS zc)
        {
            await new RepositoryFactory().BaseRepository().Insert(zc);
            string sql = "Update ZC_ZCPZMX set zt='1' where zcdm='" + zc.Zcdm + "'";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);

        }

        public static async Task UpdateZC_ZCRKS(ZC_ZCRKS zc)
        {

            await new RepositoryFactory().BaseRepository().Update(zc);

            if (zc.Zt == "2")
            {
                string sql = @"declare @c int;
                set @c=(select count(1) from ZC_ZCKP where zcdm='" + zc.Zcdm + @"');
                if @c=0 begin Update ZC_ZCKP set rksl=rksl+" + zc.Rksl + ",rkzj=rkzj+" + zc.Rkzj + ",rkdj=" + zc.Rkdj + @" where Zcdm='" + zc.Zcdm + @"' end
                else 
                INSERT INTO ZC_ZCKP
           (ListID,Dwdm ,Dwmc,Glbmdm,Glbmmc,Zcdm,Zcmc,Rksl,Rkdj,Rkzj,Skr,Rksj,Qdfsdm,Qdfsmc,Qdrq
      ,Gzrq,Zcdqr,Sfjyxzc,Cgfsdm,Cgfsmc,Yt,Pp,Ggxh,Cpxlh,Sccj,Qszm ,Xss,Qsxzmc,Trsyrq,Htbh,Bxjzrq,Fph
      ,Yjsynx,Yjsyyf,Ysxmbh,Bz,Sybmmc,Syr,Cfdd,Syzkmc,Zt,ZdrID,Zdr,Zdrq)
	  select  ListID,Dwdm ,Dwmc,Bmdm,Bmmc,Zcdm,Zcmc,Rksl,Rkdj,Rkzj,Skr,Rksj,Qdfsdm,Qdfsmc,Qdrq
      ,Gzrq,Zcdqr,Sfjyxzc,Cgfsdm,Cgfsmc,Yt,Pp,Ggxh,Cpxlh,Sccj,Qszm ,Xss,Qsxzdmc,Trsyrq,Htbh,Bxjzrq,Fph
      ,Yjsynx,Yjsyyf,Ysxmbh,Bz,Sybm,Syr,Cfdd,Syzk,'1',ZdrID,Zdr,Zdrq from ZC_ZCRKS where ListID='" + zc.ListID + "'";
                await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
                if (zc.Sfjyxzc == "是")
                {
                    ZC_JYXZC jy = new ZC_JYXZC();
                    jy.Zcdm = zc.Zcdm;

                    jy.Dwdm = zc.Dwdm;
                    jy.Dwmc = zc.Dwmc;
                    jy.Bmdm = zc.Bmdm;

                    jy.Bmmc = zc.Bmmc;
                    jy.Gldwdm = zc.Dwdm;
                    jy.Gldwmc = zc.Dwmc;
                    //jy.Jzmj = zc.Syqmj;
                    //jy.Sjkymj = zc.Syqmj;
                    await new RepositoryFactory().BaseRepository().Insert(jy);
                }
            }
        }

        public static async Task DeleteZC_ZCRKS(string listid)
        {
            string sql = "update ZC_ZCRKS set zt='0' where listid='" + listid + "'";
            sql += "declare @zcdm varchar(50);";
            sql += "set @zcdm=(select zcdm from ZC_ZCRKS where listid='" + listid + "');";
            sql += "Update ZC_ZCPZMX set zt='1' where zcdm=@zcdm;";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
            //await new RepositoryFactory().BaseRepository().Update(zc);

        }

        public static async Task<DataTable> GetZC_ZCRKSByID(string listid)
        {

            string sql = "select a.*,b.Sgdj,b.Sgsl,b.Sgzj,b.Zclbdm,b.Zclbmc from ZC_ZCRKS a left join ZC_ZCPZMX b on  a.Pzdh=b.ListID where a.ListID='" + listid + "'";

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);

        }

        #endregion

        #region "资产专用设备入库"
        public static async Task<(DataTable, int)> GetZC_ZCRKZ(ZC_ZCRKQuerry querry)
        {
            string str = " and 1=1 ";
            if (!string.IsNullOrEmpty(querry.Dwdm))
            {
                str += "and Dwdm='" + querry.Dwdm + "'";
            }
            if (!string.IsNullOrEmpty(querry.Bmdm))
            {
                str += " and bmdm='" + querry.Bmdm + "'";
            }
            if (!string.IsNullOrEmpty(querry.Zcmc))
            {
                str += " and zcmc like '%" + querry.Zcmc + "'%";
            }
            if (!string.IsNullOrEmpty(querry.Zt))
            {
                str += " and zt='" + querry.Zt + "'";
            }
            if (querry.Sbrqs != null && querry.Sbrqe != null)
            {
                if (querry.Sbrqe > querry.Sbrqs)
                {
                    str += " and (sbrq>='" + querry.Sbrqs + "' and sbrq<='" + querry.Sbrqe + "')";
                }
            }
            string sql = "select * from ZC_ZCRKZ where dwdm in " + querry.DepartID + str;
            bool isAsc = true;
            if (querry.Sort.ToUpper() != "ASC")
            {
                isAsc = false;
            }
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null, querry.SortString, isAsc, querry.Size, querry.Current);

        }

        public static async Task InsertZC_ZCRKZ(ZC_ZCRKZ zc)
        {
            await new RepositoryFactory().BaseRepository().Insert(zc);
            string sql = "Update ZC_ZCPZMX set zt='1' where listid='" + zc.Pzdh + "'";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        }

        public static async Task UpdateZC_ZCRKZ(ZC_ZCRKZ zc)
        {

            await new RepositoryFactory().BaseRepository().Update(zc);
            if (zc.Zt == "2")
            {
                string sql = @"declare @c int;
                set @c=(select count(1) from ZC_ZCKP where zcdm='" + zc.Zcdm + @"');
                if @c=0 begin Update ZC_ZCKP set rksl=rksl+" + zc.Rksj + ",rkzj=rkzj+" + zc.Rkzj + ",rkdj=" + zc.Rkdj + @" end
                else 
                INSERT INTO ZC_ZCKP
           (ListID,Dwdm ,Dwmc,Glbmdm,Glbmmc,Zcdm,Zcmc,Rksl,Rkdj,Rkzj,Skr,Rksj,Qdfsdm,Qdfsmc,Qdrq
      ,Gzrq,Zcdqr,Sfjyxzc,Cgfsdm,Cgfsmc,Yt,Pp,Ggxh,Cpxlh,Sccj,Qszm ,Xss,Qsxzmc,Trsyrq,Htbh,Bxjzrq,Fph
      ,Yjsynx,Yjsyyf,Ysxmbh,Bz,Sybmmc,Syr,Cfdd,Syzkmc,Zt,ZdrID,Zdr,Zdrq)
	  select  ListID,Dwdm ,Dwmc,Bmdm,Bmmc,Zcdm,Zcmc,Rksl,Rkdj,Rkzj,Skr,Rksj,Qdfsdm,Qdfsmc,Qdrq
      ,Gzrq,Zcdqr,Sfjyxzc,Cgfsdm,Cgfsmc,Yt,Pp,Ggxh,Cpxlh,Sccj,Qszm ,Xss,Qsxzdmc,Trsyrq,Htbh,Bxjzrq,Fph
      ,Yjsynx,Yjsyyf,Ysxmbh,Bz,Sybm,Syr,Cfdd,Syzk,'1',ZdrID,Zdr,Zdrq from ZC_ZCRKZ where ListID='" + zc.ListID + "'";
                await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
                if (zc.Sfjyxzc == "是")
                {
                    ZC_JYXZC jy = new ZC_JYXZC();
                    jy.Zcdm = zc.Zcdm;

                    jy.Dwdm = zc.Dwdm;
                    jy.Dwmc = zc.Dwmc;
                    jy.Bmdm = zc.Bmdm;
                    jy.Bmmc = zc.Bmmc;
                    jy.Gldwdm = zc.Dwdm;
                    jy.Gldwmc = zc.Dwmc;

                    await new RepositoryFactory().BaseRepository().Insert(jy);
                }
            }
        }

        public static async Task DeleteZC_ZCRKZ(string listid)
        {
            string sql = "update ZC_ZCRKZ set zt='0' where listid='" + listid + "'";
            sql += "declare @zcdm varchar(50);";
            sql += "set @zcdm=(select zcdm from ZC_ZCRKZ where listid='" + listid + "');";
            sql += "Update ZC_ZCPZMX set zt='1' where zcdm=@zcdm;";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
            //await new RepositoryFactory().BaseRepository().Update(zc);

        }

        public static async Task<DataTable> GetZC_ZCRKZByID(string listid)
        {

            string sql = "select a.*,b.Sgdj,b.Sgsl,b.Sgzj,b.Zclbdm,b.Zclbmc from ZC_ZCRKZ a left join ZC_ZCPZMX b on  a.Pzdh=b.ListID where a.ListID='" + listid + "'";

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);

        }

        #endregion

        #region "资产文物入库"
        public static async Task<(DataTable, int)> GetZC_ZCRKW(ZC_ZCRKQuerry querry)
        {
            string str = " and 1=1 ";
            if (!string.IsNullOrEmpty(querry.Dwdm))
            {
                str += "and Dwdm='" + querry.Dwdm + "'";
            }
            if (!string.IsNullOrEmpty(querry.Bmdm))
            {
                str += " and bmdm='" + querry.Bmdm + "'";
            }
            if (!string.IsNullOrEmpty(querry.Zcmc))
            {
                str += " and zcmc like '%" + querry.Zcmc + "'%";
            }
            if (!string.IsNullOrEmpty(querry.Zt))
            {
                str += " and zt='" + querry.Zt + "'";
            }
            if (querry.Sbrqs != null && querry.Sbrqe != null)
            {
                if (querry.Sbrqe > querry.Sbrqs)
                {
                    str += " and (sbrq>='" + querry.Sbrqs + "' and sbrq<='" + querry.Sbrqe + "')";
                }
            }
            string sql = "select * from ZC_ZCRKW where dwdm in " + querry.DepartID + str;
            bool isAsc = true;
            if (querry.Sort.ToUpper() != "ASC")
            {
                isAsc = false;
            }
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null, querry.SortString, isAsc, querry.Size, querry.Current);

        }

        public static async Task InsertZC_ZCRKW(ZC_ZCRKW zc)
        {
            await new RepositoryFactory().BaseRepository().Insert(zc);
            string sql = "Update ZC_ZCPZMX set zt='1' where ListID='" + zc.Pzdh + "'";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        }

        public static async Task UpdateZC_ZCRKW(ZC_ZCRKW zc)
        {

            await new RepositoryFactory().BaseRepository().Update(zc);
            if (zc.Zt == "2")
            {
                string sql = @"declare @c int;
                set @c=(select count(1) from ZC_ZCKP where zcdm='" + zc.Zcdm + @"');
                if @c=0 begin Update ZC_ZCKP set rksl=rksl+" + zc.Rksj + ",rkzj=rkzj+" + zc.Rkzj + ",rkdj=" + zc.Rkdj + @" end
                else 
                INSERT INTO ZC_ZCKP
           (ListID,Dwdm,Dwmc,Glbmdm,Glbmmc ,Zcdm,Zcmc,Rksl,Rkdj,Rkzj,Skr,Rksj,Qdfsdm,Qdfsmc,Qdrq,Gzrq,Cgfsdm
      ,Cgfsmc,Sfjyxzc,Cpnd,Lyd,Wwdjdm,Wwdjmc,Ysxmbh,Bz,Sybmmc,Cfdd,Syzkmc,Zt,ZdrID,Zdr,Zdrq)
	  select  ListID,Dwdm,Dwmc,Bmdm,Bmmc ,Zcdm,Zcmc,Rksl,Rkdj,Rkzj,Skr,Rksj,Qdfsdm,Qdfsmc,Qdrq,Gzrq,Cgfsdm
      ,Cgfsmc,Sfjyxzc,Cpnd,Lyd,Wwdjdm,Wwdjmc,Ysxmbh,Bz,Sybm,Cfdd,Syzk,'1',ZdrID,Zdr,Zdrq from ZC_ZCRKW where ListID='" + zc.ListID + "'";
                await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
                if (zc.Sfjyxzc == "是")
                {
                    ZC_JYXZC jy = new ZC_JYXZC();
                    jy.Zcdm = zc.Zcdm;

                    jy.Dwdm = zc.Dwdm;
                    jy.Dwmc = zc.Dwmc;
                    jy.Bmdm = zc.Bmdm;
                    jy.Bmmc = zc.Bmmc;
                    jy.Gldwdm = zc.Dwdm;
                    jy.Gldwmc = zc.Dwmc;

                    await new RepositoryFactory().BaseRepository().Insert(jy);
                }
            }
        }

        public static async Task DeleteZC_ZCRKW(string listid)
        {
            string sql = "update ZC_ZCRKW set zt='0' where listid='" + listid + "'";
            sql += "declare @zcdm varchar(50);";
            sql += "set @zcdm=(select zcdm from ZC_ZCRKW where listid='" + listid + "');";
            sql += "Update ZC_ZCPZMX set zt='1' where zcdm=@zcdm;";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
            //await new RepositoryFactory().BaseRepository().Update(zc);

        }

        public static async Task<DataTable> GetZC_ZCRKWByID(string listid)
        {

            string sql = "select a.*,b.Sgdj,b.Sgsl,b.Sgzj,b.Zclbdm,b.Zclbmc from ZC_ZCRKW a left join ZC_ZCPZMX b on  a.Pzdh=b.ListID where a.ListID='" + listid + "'";

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);

        }

        #endregion

        #region "资产图书档案入库"
        public static async Task<(DataTable, int)> GetZC_ZCRKD(ZC_ZCRKQuerry querry)
        {
            string str = " and 1=1 ";
            if (!string.IsNullOrEmpty(querry.Dwdm))
            {
                str += "and Dwdm='" + querry.Dwdm + "'";
            }
            if (!string.IsNullOrEmpty(querry.Bmdm))
            {
                str += " and bmdm='" + querry.Bmdm + "'";
            }
            if (!string.IsNullOrEmpty(querry.Zcmc))
            {
                str += " and zcmc like '%" + querry.Zcmc + "'%";
            }
            if (!string.IsNullOrEmpty(querry.Zt))
            {
                str += " and zt='" + querry.Zt + "'";
            }
            if (querry.Sbrqs != null && querry.Sbrqe != null)
            {
                if (querry.Sbrqe > querry.Sbrqs)
                {
                    str += " and (sbrq>='" + querry.Sbrqs + "' and sbrq<='" + querry.Sbrqe + "')";
                }
            }
            string sql = "select * from ZC_ZCRKD where dwdm in " + querry.DepartID + str;
            bool isAsc = true;
            if (querry.Sort.ToUpper() != "ASC")
            {
                isAsc = false;
            }
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null, querry.SortString, isAsc, querry.Size, querry.Current);

        }

        public static async Task InsertZC_ZCRKD(ZC_ZCRKD zc)
        {
            await new RepositoryFactory().BaseRepository().Insert(zc);
            string sql = "Update ZC_ZCPZMX set zt='1' where ListID='" + zc.Pzdh + "'";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        }

        public static async Task UpdateZC_ZCRKD(ZC_ZCRKD zc)
        {

            await new RepositoryFactory().BaseRepository().Update(zc);
            if (zc.Zt == "2")
            {
                string sql = @"declare @c int;
                set @c=(select count(1) from ZC_ZCKP where zcdm='" + zc.Zcdm + @"');
                if @c=0 begin Update ZC_ZCKP set rksl=rksl+" + zc.Rksj + ",rkzj=rkzj+" + zc.Rkzj + ",rkdj=" + zc.Rkdj + @" end
                else 
                INSERT INTO ZC_ZCKP
           (ListID,Dwdm,Dwmc,Glbmdm,Glbmmc,Zcdm,Zcmc ,Rksl,Rkdj,Rkzj,Skr,Rksj,Qdfsdm,Qdfsmc,Qdrq,Gzrq,Cgfsdm,Cgfsmc
      ,Sfjyxzc,Cbs,Cbrq,Trsyrq,Dah ,Ysxmbh,Bz,Sybmmc ,Syr,Cfdd,Syzkmc,Zt,ZdrID,Zdr,Zdrq)
	  select  ListID,Dwdm,Dwmc,Bmdm,Bmmc,Zcdm,Zcmc ,Rksl,Rkdj,Rkzj,Skr,Rksj,Qdfsdm,Qdfsmc,Qdrq,Gzrq,Cgfsdm,Cgfsmc
      ,Sfjyxzc,Cbs,Cbrq,Trsyrq,Dah ,Ysxmbh,Bz,Sybm ,Syr,Cfdd,Syzk,'1',ZdrID,Zdr,Zdrq from ZC_ZCRKD where ListID='" + zc.ListID + "'";
                await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
                if (zc.Sfjyxzc == "是")
                {
                    ZC_JYXZC jy = new ZC_JYXZC();
                    jy.Zcdm = zc.Zcdm;

                    jy.Dwdm = zc.Dwdm;
                    jy.Dwmc = zc.Dwmc;
                    jy.Bmdm = zc.Bmdm;
                    jy.Bmmc = zc.Bmmc;
                    jy.Gldwdm = zc.Dwdm;
                    jy.Gldwmc = zc.Dwmc;

                    await new RepositoryFactory().BaseRepository().Insert(jy);
                }
            }
        }

        public static async Task DeleteZC_ZCRKD(string listid)
        {
            string sql = "update ZC_ZCRKD set zt='0' where listid='" + listid + "'";
            sql += "declare @zcdm varchar(50);";
            sql += "set @zcdm=(select zcdm from ZC_ZCRKD where listid='" + listid + "');";
            sql += "Update ZC_ZCPZMX set zt='1' where zcdm=@zcdm;";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
            //await new RepositoryFactory().BaseRepository().Update(zc);

        }

        public static async Task<DataTable> GetZC_ZCRKDByID(string listid)
        {

            string sql = "select a.*,b.Sgdj,b.Sgsl,b.Sgzj,b.Zclbdm,b.Zclbmc from ZC_ZCRKD a left join ZC_ZCPZMX b on  a.Pzdh=b.ListID where a.ListID='" + listid + "'";

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);

        }

        #endregion

        #region "资产家具档案入库"
        public static async Task<(DataTable, int)> GetZC_ZCRKY(ZC_ZCRKQuerry querry)
        {
            string str = " and 1=1 ";
            if (!string.IsNullOrEmpty(querry.Dwdm))
            {
                str += "and Dwdm='" + querry.Dwdm + "'";
            }
            if (!string.IsNullOrEmpty(querry.Bmdm))
            {
                str += " and bmdm='" + querry.Bmdm + "'";
            }
            if (!string.IsNullOrEmpty(querry.Zcmc))
            {
                str += " and zcmc like '%" + querry.Zcmc + "'%";
            }
            if (!string.IsNullOrEmpty(querry.Zt))
            {
                str += " and zt='" + querry.Zt + "'";
            }
            if (querry.Sbrqs != null && querry.Sbrqe != null)
            {
                if (querry.Sbrqe > querry.Sbrqs)
                {
                    str += " and (sbrq>='" + querry.Sbrqs + "' and sbrq<='" + querry.Sbrqe + "')";
                }
            }
            string sql = "select * from ZC_ZCRKY where dwdm in " + querry.DepartID + str;
            bool isAsc = true;
            if (querry.Sort.ToUpper() != "ASC")
            {
                isAsc = false;
            }
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null, querry.SortString, isAsc, querry.Size, querry.Current);

        }

        public static async Task InsertZC_ZCRKY(ZC_ZCRKY zc)
        {
            await new RepositoryFactory().BaseRepository().Insert(zc);
            string sql = "Update ZC_ZCPZMX set zt='1' where ListID='" + zc.Pzdh + "'";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        }

        public static async Task UpdateZC_ZCRKY(ZC_ZCRKY zc)
        {

            await new RepositoryFactory().BaseRepository().Update(zc);

            if (zc.Zt == "2")
            {
                string sql = @"declare @c int;
                set @c=(select count(1) from ZC_ZCKP where zcdm='" + zc.Zcdm + @"');
                if @c=0 begin Update ZC_ZCKP set rksl=rksl+" + zc.Rksj + ",rkzj=rkzj+" + zc.Rkzj + ",rkdj=" + zc.Rkdj + @" end
                else 
                INSERT INTO ZC_ZCKP
           (ListID,Dwdm,Dwmc,Glbmdm,Glbmmc,Zcdm ,Zcmc,Rksl,Rkdj,Rkzj,Skr,Rksj,Qdfsdm,Qdfsmc ,Qdrq,Gzrq,Zcdqr,Cgfsdm,Cgfsmc,Sfjyxzc,Ggxh
      ,Pp,Xss,Trsyrq,Yjsynx ,Yjsyyf,Bxjzrq,Ysxmbh,Bz ,Sybmmc,Cfdd,Syzkmc ,Zt,ZdrID,Zdr,Zdrq)
	  select  ListID,Dwdm,Dwmc,Bmdm,Bmmc,Zcdm ,Zcmc,Rksl,Rkdj,Rkzj,Skr,Rksj,Qdfsdm,Qdfsmc ,Qdrq,Gzrq,Zcdqrq,Cgfsdm,Cgfsmc,Sfjyxzc,Ggxh
      ,Pp,Xss,Trsyrq,Yjsynx ,Yjsyyf,Bxjzrq,Ysxmbh,Bz ,Sybm,Cfdd,Syzk ,'1',ZdrID,Zdr,Zdrq from ZC_ZCRKY where ListID='" + zc.ListID + "'";
                await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
                if (zc.Sfjyxzc == "是")
                {
                    ZC_JYXZC jy = new ZC_JYXZC();
                    jy.Zcdm = zc.Zcdm;

                    jy.Dwdm = zc.Dwdm;
                    jy.Dwmc = zc.Dwmc;
                    jy.Bmdm = zc.Bmdm;
                    jy.Bmmc = zc.Bmmc;

                    jy.Gldwdm = zc.Dwdm;
                    jy.Gldwmc = zc.Dwmc;

                    await new RepositoryFactory().BaseRepository().Insert(jy);
                }
            }
        }

        public static async Task DeleteZC_ZCRKY(string listid)
        {
            string sql = "update ZC_ZCRKY set zt='0' where listid='" + listid + "';";
            sql += "declare @zcdm varchar(50);";
            sql += "set @zcdm=(select zcdm from ZC_ZCRKY where listid='" + listid + "');";
            sql += "Update ZC_ZCPZMX set zt='1' where zcdm=@zcdm;";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
            //await new RepositoryFactory().BaseRepository().Update(zc);

        }

        public static async Task<DataTable> GetZC_ZCRKYByID(string listid)
        {

            string sql = "select a.*,b.Sgdj,b.Sgsl,b.Sgzj,b.Zclbdm,b.Zclbmc from ZC_ZCRKY a left join ZC_ZCPZMX b on  a.Pzdh=b.ListID where a.ListID='" + listid + "'";

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);

        }

        #endregion

        #region "资产领用"
        public static async Task<(DataTable, int)> GetZC_ZCLY(ZC_ZCLYQuerry querry)
        {
            string str = " and 1=1 ";
            if (!string.IsNullOrEmpty(querry.Dwdm) && querry.Dwdm != "string")
            {
                str += "and Dwdm='" + querry.Dwdm + "'";
            }
            if (!string.IsNullOrEmpty(querry.Bmdm) && querry.Bmdm != "string")
            {
                str += " and bmdm='" + querry.Bmdm + "'";
            }
            if (!string.IsNullOrEmpty(querry.Zcdl) && querry.Zcdl != "string")
            {
                str += " and Zcdl like '%" + querry.Zcdl + "%'";
            }
            if (!string.IsNullOrEmpty(querry.Zt) && querry.Zt != "string")
            {
                str += " and zt='" + querry.Zt + "'";
            }
            if (querry.Sbrqs != null && querry.Sbrqe != null)
            {
                if (querry.Sbrqe > querry.Sbrqs)
                {
                    str += " and (sbrq>='" + querry.Sbrqs + "' and sbrq<='" + querry.Sbrqe + "')";
                }
            }
            string sql = "select * from ZC_ZCLY where dwdm in " + querry.DepartID + " and zt!='0' " + str;
            bool isAsc = true;
            if (querry.Sort.ToUpper() != "ASC")
            {
                isAsc = false;
            }
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null, querry.SortString, isAsc, querry.Size, querry.Current);

        }

        public static async Task<(DataTable, int)> GetKLZC(LYZCQuerry querry)
        {
            string where = " and 1=1 and rksl>0";
            if (!string.IsNullOrEmpty(querry.Zcmc) && querry.Zcmc != "string")
            {
                where += " and zcmc like '%" + querry.Zcmc + "%'";
            }
            if (querry.Zcdl != "")
            {
                where += "and zcdl='" + querry.Zcdl + "'";
            }
            if (querry.End > querry.Start)
            {
                where += "and Rksj>='" + querry.Start + "' and Rksj<='" + querry.End + "'";
            }
            //string sql = "";
            string sql = @" select * from(
                         select a.ListID, a.Zcdm, a.Zcmc, b.Zclbdm, b.Zclbmc, Rksj, b.Ggxh, b.Jldw, a.Zt,'1' as zcdl,a.Dwdm,rksl,Rkdj,Rkzj,skr from ZC_ZCRKJ a left
                                 join ZC_ZCPZMX b on a.Pzdh = b.ListID
                        union
                        select a.ListID,a.Zcdm,a.Zcmc,b.Zclbdm,b.Zclbmc,Rksj,b.Ggxh,b.Jldw,a.Zt,'2' as zcdl,a.Dwdm,rksl,Rkdj,Rkzj,skr from ZC_ZCRKS a left
                        join ZC_ZCPZMX b on a.Pzdh = b.ListID
                        union
                        select a.ListID,a.Zcdm,a.Zcmc,b.Zclbdm,b.Zclbmc,Rksj,b.Ggxh,b.Jldw,a.Zt,'3' as zcdl,a.Dwdm,rksl,Rkdj,Rkzj,skr from ZC_ZCRKZ a left
                        join ZC_ZCPZMX b on a.Pzdh = b.ListID
                        union
                        select a.ListID,a.Zcdm,a.Zcmc,b.Zclbdm,b.Zclbmc,Rksj,b.Ggxh,b.Jldw,a.Zt,'4' as zcdl,a.Dwdm,rksl,Rkdj,Rkzj,skr from ZC_ZCRKW a left
                        join ZC_ZCPZMX b on a.Pzdh = b.ListID
                        union
                        select a.ListID,a.Zcdm,a.Zcmc,b.Zclbdm,b.Zclbmc,Rksj,b.Ggxh,b.Jldw,a.Zt,'5' as zcdl,a.Dwdm,rksl,Rkdj,Rkzj,skr from ZC_ZCRKD a left
                        join ZC_ZCPZMX b on a.Pzdh = b.ListID
                        union
                        select a.ListID,a.Zcdm,a.Zcmc,b.Zclbdm,b.Zclbmc,Rksj,b.Ggxh,b.Jldw,a.Zt,'6' as zcdl,a.Dwdm,rksl,Rkdj,Rkzj,skr from ZC_ZCRKY a left join ZC_ZCPZMX b on a.Pzdh = b.ListID) as c";
            sql += " where dwdm in " + querry.DepartID + where;
            bool isAsc = true;
            if (querry.Sort.ToUpper() != "ASC")
            {
                isAsc = false;
            }
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null, querry.SortString, isAsc, querry.Size, querry.Current);
        }

        public static async Task<DataTable> GetKLZCByID(String Listid)
        {
            string sql = @" select * from(
                         select a.ListID, a.Zcdm, a.Zcmc, b.Zclbdm, b.Zclbmc, Rksj, b.Ggxh, b.Jldw, a.Zt,'1' as zcdl,a.Dwdm,rksl,Rkdj,Rkzj,skr from ZC_ZCRKJ a left
                                 join ZC_ZCPZMX b on a.Pzdh = b.ListID
                        union
                        select a.ListID,a.Zcdm,a.Zcmc,b.Zclbdm,b.Zclbmc,Rksj,b.Ggxh,b.Jldw,a.Zt,'2' as zcdl,a.Dwdm,rksl,Rkdj,Rkzj,skr from ZC_ZCRKS a left
                        join ZC_ZCPZMX b on a.Pzdh = b.ListID
                        union
                        select a.ListID,a.Zcdm,a.Zcmc,b.Zclbdm,b.Zclbmc,Rksj,b.Ggxh,b.Jldw,a.Zt,'3' as zcdl,a.Dwdm,rksl,Rkdj,Rkzj,skr from ZC_ZCRKZ a left
                        join ZC_ZCPZMX b on a.Pzdh = b.ListID
                        union
                        select a.ListID,a.Zcdm,a.Zcmc,b.Zclbdm,b.Zclbmc,Rksj,b.Ggxh,b.Jldw,a.Zt,'4' as zcdl,a.Dwdm,rksl,Rkdj,Rkzj,skr from ZC_ZCRKW a left
                        join ZC_ZCPZMX b on a.Pzdh = b.ListID
                        union
                        select a.ListID,a.Zcdm,a.Zcmc,b.Zclbdm,b.Zclbmc,Rksj,b.Ggxh,b.Jldw,a.Zt,'5' as zcdl,a.Dwdm,rksl,Rkdj,Rkzj,skr from ZC_ZCRKD a left
                        join ZC_ZCPZMX b on a.Pzdh = b.ListID
                        union
                        select a.ListID,a.Zcdm,a.Zcmc,b.Zclbdm,b.Zclbmc,Rksj,b.Ggxh,b.Jldw,a.Zt,'6' as zcdl,a.Dwdm,rksl,Rkdj,Rkzj,skr from ZC_ZCRKY a left join ZC_ZCPZMX b on a.Pzdh = b.ListID) as c";
            sql += " where ListID='" + Listid + "'";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }

        public static async Task<DataTable> GetZCXQ(ZCDLEntity list)
        {
            string table = "";
            switch (list.dldm)
            {
                case "1":
                    table = "ZC_ZCRKJ";
                    break;
                case "2":
                    table = "ZC_ZCRKS";
                    break;
                case "3":
                    table = "ZC_ZCRKZ";
                    break;
                case "4":
                    table = "ZC_ZCRKW";
                    break;
                case "5":
                    table = "ZC_ZCRKD";
                    break;
                case "6":
                    table = "ZC_ZCRKY";
                    break;
            }
            string sql = "select a.*,b.Zclbdm, b.Zclbmc, b.Ggxh, b.Jldw from " + table + " a left join ZC_ZCPZMX b on a.Pzdh=b.ListID where a.Listid='" + list.pid + "'";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }

        public static async Task SaveZC_ZCLYMX(ListEntity list)
        {
            string sql = "";
            int index = 0;
            foreach (string cid in list.cid)
            {
                string table = "";
                switch (list.dldm)
                {
                    case "1":
                        table = "ZC_ZCRKJ";
                        break;
                    case "2":
                        table = "ZC_ZCRKS";
                        break;
                    case "3":
                        table = "ZC_ZCRKZ";
                        break;
                    case "4":
                        table = "ZC_ZCRKW";
                        break;
                    case "5":
                        table = "ZC_ZCRKD";
                        break;
                    case "6":
                        table = "ZC_ZCRKY";
                        break;
                }
                sql += "insert into ZC_ZCLYMX(ListID,LyID,RkID,Zcdm,Zcmc,Ggxh,Sl,Jldw,Cfdd)" +
                    " select '" + DateTime.Now.ToString("yyyyMMddHHmmssffffff") + index + "','" + list.pid + "','" + cid + "'," +
                    "a.Zcdm,a.Zcmc,b.Ggxh,0,Jldw,a.Cfdd from " + table + " a left join ZC_ZCPZMX b on a.Pzdh=b.ListID where a.Listid='" + cid + "';";
                index++;
            }
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        }
        public static async Task InsertZC_ZCLY(ZC_ZCLY zc)
        {
            await new RepositoryFactory().BaseRepository().Insert(zc);

        }

        public static async Task UpdateZC_ZCLY(ZC_ZCLY zc)
        {
            await new RepositoryFactory().BaseRepository().Update(zc);
            if (zc.Zt == "2")
            {
                string sql = "declare @lysl decimal(20,2);declare @rkid varchar(50);";
                sql += "declare @zcdl varchar(1);set @zcdl=" + zc.Zcdl;
                sql += @"declare ColCur cursor for select Sl,RkID from ZC_ZCLYMX where lyid='" + zc.ListID + @"';
                open ColCur;
                        FETCH NEXT FROM ColCur INTO @lysl,@rkid;
                        WHILE(@@FETCH_STATUS = 0)
                        BEGIN
                           if @zcdl='1'
                            begin
                                update ZC_ZCRKJ set Rksl=Rksl-@lysl where listid=@rkid
                            end
                           if @zcdl='2'
                            begin
                                update ZC_ZCRKS set Rksl=Rksl-@lysl where listid=@rkid
                            end
                            if @zcdl='3'
                            begin
                                update ZC_ZCRKZ set Rksl=Rksl-@lysl where listid=@rkid
                            end
                            if @zcdl='4'
                            begin
                                update ZC_ZCRKW set Rksl=Rksl-@lysl where listid=@rkid
                            end
                            if @zcdl='5'
                            begin
                                update ZC_ZCRKD set Rksl=Rksl-@lysl where listid=@rkid
                            end
                            if @zcdl='6'
                            begin
                                update ZC_ZCRKY set Rksl=Rksl-@lysl where listid=@rkid
                            end
                        FETCH NEXT FROM ColCur INTO @lysl,@rkid;
                        END
                        close ColCur;
                        deallocate ColCur; ";
                await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
            }
        }

        public static async Task UpdateZC_LYZT(ListStatus status)
        {
            string sql = "Update ZC_ZCLY set Zt='" + status.Zt + "' where ListID='" + status.ListID + "';";
            if (status.Zt == "2")
            {
                sql += "declare @lysl decimal(20,2);declare @rkid varchar(50);";
                sql += "declare @zcdl varchar(1);";
                sql += "select @zcdl=zcdl from ZC_ZCLY where ListID='" + status.ListID + "';";
                //sql += "select @lysl=Sl,@rkid=RkID from ZC_ZCLYMX where lyid='" + status.ListID + "';";

                sql += @"declare ColCur cursor for select Sl,RkID from ZC_ZCLYMX where lyid='" + status.ListID + @"';
                open ColCur;
                        FETCH NEXT FROM ColCur INTO @lysl,@rkid;
                        WHILE(@@FETCH_STATUS = 0)
                        BEGIN
                           if @zcdl='1'
                            begin
                                update ZC_ZCRKJ set Rksl=Rksl-@lysl where listid=@rkid
                            end
                           if @zcdl='2'
                            begin
                                update ZC_ZCRKS set Rksl=Rksl-@lysl where listid=@rkid
                            end
                            if @zcdl='3'
                            begin
                                update ZC_ZCRKZ set Rksl=Rksl-@lysl where listid=@rkid
                            end
                            if @zcdl='4'
                            begin
                                update ZC_ZCRKW set Rksl=Rksl-@lysl where listid=@rkid
                            end
                            if @zcdl='5'
                            begin
                                update ZC_ZCRKD set Rksl=Rksl-@lysl where listid=@rkid
                            end
                            if @zcdl='6'
                            begin
                                update ZC_ZCRKY set Rksl=Rksl-@lysl where listid=@rkid
                            end
                        FETCH NEXT FROM ColCur INTO @lysl,@rkid;
                        END
                        close ColCur;
                        deallocate ColCur; ";


                //await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
            }

            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        }

        public static async Task DeleteZC_ZCLY(string Listid)
        {
            //await new RepositoryFactory().BaseRepository().Update(zc);
            string sql = "update ZC_ZCLY set zt='0' where ListID='" + Listid + "';";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        }

        public static async Task<DataTable> GetZC_ZCLYByID(string listid)
        {

            string sql = "select * from ZC_ZCLY where ListID='" + listid + "'";

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);

        }

        public static async Task<DataTable> GetZC_ZCLYMX(string Lyid)
        {

            string sql = @"select d.*,c.Rkdj,c.Rkzj,c.rksl, c.Zclbdm,c.Zclbmc,Skr,Rksj,zcdl from ZC_ZCLYMX d left join(
                         select a.ListID, a.Zcdm, a.Zcmc, b.Zclbdm, b.Zclbmc, Rksj, b.Ggxh, b.Jldw, a.Zt,'1' as zcdl,a.Dwdm,rksl,Rkdj,Rkzj,Skr from ZC_ZCRKJ a left
                                 join ZC_ZCPZMX b on a.Pzdh = b.ListID
                        union
                        select a.ListID,a.Zcdm,a.Zcmc,b.Zclbdm,b.Zclbmc,Rksj,b.Ggxh,b.Jldw,a.Zt,'2' as zcdl,a.Dwdm,rksl,Rkdj,Rkzj,Skr from ZC_ZCRKS a left
                        join ZC_ZCPZMX b on a.Pzdh = b.ListID
                        union
                        select a.ListID,a.Zcdm,a.Zcmc,b.Zclbdm,b.Zclbmc,Rksj,b.Ggxh,b.Jldw,a.Zt,'3' as zcdl,a.Dwdm,rksl,Rkdj,Rkzj,Skr from ZC_ZCRKZ a left
                        join ZC_ZCPZMX b on a.Pzdh = b.ListID
                        union
                        select a.ListID,a.Zcdm,a.Zcmc,b.Zclbdm,b.Zclbmc,Rksj,b.Ggxh,b.Jldw,a.Zt,'4' as zcdl,a.Dwdm,rksl,Rkdj,Rkzj ,Skr from ZC_ZCRKW a left
                        join ZC_ZCPZMX b on a.Pzdh = b.ListID
                        union
                        select a.ListID,a.Zcdm,a.Zcmc,b.Zclbdm,b.Zclbmc,Rksj,b.Ggxh,b.Jldw,a.Zt,'5' as zcdl,a.Dwdm,rksl,Rkdj,Rkzj,Skr from ZC_ZCRKD a left
                        join ZC_ZCPZMX b on a.Pzdh = b.ListID
                        union
                        select a.ListID,a.Zcdm,a.Zcmc,b.Zclbdm,b.Zclbmc,Rksj,b.Ggxh,b.Jldw,a.Zt,'6' as zcdl,a.Dwdm,rksl,Rkdj,Rkzj,Skr from ZC_ZCRKY a left join ZC_ZCPZMX b on a.Pzdh = b.ListID) as c on d.RkID = c.ListID where LyID='" + Lyid + "'";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);

        }

        public static async Task<DataTable> GetZC_ZCLYMXByID(string Listid)
        {

            string sql = @"select d.*,c.Rkdj,c.Rkzj,c.rksl,c.Zclbdm,c.Zclbmc,Skr,Rksj,Sgdj,Sgsl,Sgze,Yt,Qdfsdm,Qdfsmc,Qdrq,Gzrq,Zcdqr,
 Trsyrq,Yjsynx,Yjsyyf,Ysxmbh,Bz,Sfjyxzc from ZC_ZCLYMX d left join(
                          select a.ListID, a.Zcdm, a.Zcmc, b.Zclbdm, b.Zclbmc, Rksj, b.Ggxh, b.Jldw, a.Zt,'1' as zcdl,a.Dwdm,rksl,Rkdj,Rkzj,Skr,Sgdj,Sgsl,Sgze,a.Yt,a.Qdfsdm,a.Qdfsmc,a.Qdrq,a.Gzrq,a.Zcdqr,
 a.Trsyrq,e.Yjsynx,e.Yjsyyf,a.Ysxmbh,a.Bz,a.Sfjyxzc from ZC_ZCRKJ a left
join ZC_ZCPZMX b on a.Pzdh = b.ListID left join ZC_CGML e on b.Zclbdm=e.Mldm
union
select a.ListID, a.Zcdm, a.Zcmc, b.Zclbdm, b.Zclbmc, Rksj, b.Ggxh, b.Jldw, a.Zt,'1' as zcdl,a.Dwdm,rksl,Rkdj,Rkzj,Skr,Sgdj,Sgsl,Sgze,a.Yt,a.Qdfsdm,a.Qdfsmc,a.Qdrq,a.Gzrq,a.Zcdqr,
 a.Trsyrq,e.Yjsynx,e.Yjsyyf,a.Ysxmbh,a.Bz,a.Sfjyxzc from ZC_ZCRKS a left
join ZC_ZCPZMX b on a.Pzdh = b.ListID left join ZC_CGML e on b.Zclbdm=e.Mldm
                        union
                        select a.ListID, a.Zcdm, a.Zcmc, b.Zclbdm, b.Zclbmc, Rksj, b.Ggxh, b.Jldw, a.Zt,'1' as zcdl,a.Dwdm,rksl,Rkdj,Rkzj,Skr,Sgdj,Sgsl,Sgze,a.Yt,a.Qdfsdm,a.Qdfsmc,a.Qdrq,a.Gzrq,a.Zcdqr,
 a.Trsyrq,e.Yjsynx,e.Yjsyyf,a.Ysxmbh,a.Bz,a.Sfjyxzc from ZC_ZCRKZ a left
                        join ZC_ZCPZMX b on a.Pzdh = b.ListID left join ZC_CGML e on b.Zclbdm=e.Mldm
                        union
                        select a.ListID, a.Zcdm, a.Zcmc, b.Zclbdm, b.Zclbmc, Rksj, b.Ggxh, b.Jldw, a.Zt,'1' as zcdl,a.Dwdm,rksl,Rkdj,Rkzj,Skr,Sgdj,Sgsl,Sgze,b.Yt,a.Qdfsdm,a.Qdfsmc,a.Qdrq,a.Gzrq,NULL,
 NULL,e.Yjsynx,e.Yjsyyf,a.Ysxmbh,a.Bz,a.Sfjyxzc from ZC_ZCRKW a left
                        join ZC_ZCPZMX b on a.Pzdh = b.ListID left join ZC_CGML e on b.Zclbdm=e.Mldm
                        union
                        select a.ListID, a.Zcdm, a.Zcmc, b.Zclbdm, b.Zclbmc, Rksj, b.Ggxh, b.Jldw, a.Zt,'1' as zcdl,a.Dwdm,rksl,Rkdj,Rkzj,Skr,Sgdj,Sgsl,Sgze,b.Yt,a.Qdfsdm,a.Qdfsmc,a.Qdrq,a.Gzrq,NULL,
 a.Trsyrq,e.Yjsynx,e.Yjsyyf,a.Ysxmbh,a.Bz,a.Sfjyxzc from ZC_ZCRKD a left
                        join ZC_ZCPZMX b on a.Pzdh = b.ListID left join ZC_CGML e on b.Zclbdm=e.Mldm
                        union
                        select a.ListID, a.Zcdm, a.Zcmc, b.Zclbdm, b.Zclbmc, Rksj, b.Ggxh, b.Jldw, a.Zt,'1' as zcdl,a.Dwdm,rksl,Rkdj,Rkzj,Skr,Sgdj,Sgsl,Sgze,b.Yt,a.Qdfsdm,a.Qdfsmc,a.Qdrq,a.Gzrq,NULL,
 a.Trsyrq,e.Yjsynx,e.Yjsyyf,a.Ysxmbh,a.Bz,a.Sfjyxzc from ZC_ZCRKY a left join ZC_ZCPZMX b on a.Pzdh = b.ListID left join ZC_CGML e on b.Zclbdm=e.Mldm) as c on d.RkID = c.ListID where d.ListID='" + Listid + "'";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);

        }
        public static async Task InsertZC_ZCLYMX(ZC_ZCLYMX zc)
        {
            await new RepositoryFactory().BaseRepository().Insert(zc);

        }

        public static async Task UpdateZC_ZCLYMX(ZC_ZCLYMX zc)
        {
            string sql = "Update ZC_ZCLYMX set sl=" + zc.Sl + " where ListID='" + zc.ListID + "';";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        }

        public static async Task DeleteZC_ZCLYMX(string Listid)
        {
            string sql = "delete from ZC_ZCLYMX where ListID='" + Listid + "'";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        }


        #endregion

        #region "资产变动"
        public static async Task<(DataTable, int)> GetZC_ZCBD(ZC_ZCBDQuerry querry)
        {
            string str = " and 1=1 ";
            if (!string.IsNullOrEmpty(querry.Dwdm) && querry.Dwdm != "string")
            {
                str += "and Dwdm='" + querry.Dwdm + "'";
            }
            if (!string.IsNullOrEmpty(querry.Bmdm) && querry.Bmdm != "string")
            {
                str += " and bmdm='" + querry.Bmdm + "'";
            }

            if (!string.IsNullOrEmpty(querry.Zt) && querry.Zt != "string")
            {
                str += " and zt='" + querry.Zt + "'";
            }
            if (querry.Sbrqs != null && querry.Sbrqe != null)
            {
                if (querry.Sbrqe > querry.Sbrqs)
                {
                    str += " and (Sqrq>='" + querry.Sbrqs + "' and Sqrq<='" + querry.Sbrqe + "')";
                }
            }
            string sql = "select * from ZC_ZCBD where dwdm in " + querry.DepartID + str;
            bool isAsc = true;
            if (querry.Sort.ToUpper() != "ASC")
            {
                isAsc = false;
            }
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null, querry.SortString, isAsc, querry.Size, querry.Current);

        }

        public static async Task InsertZC_ZCBD(ZC_ZCBD zc)
        {
            await new RepositoryFactory().BaseRepository().Insert(zc);

        }

        public static async Task UpdateZC_ZCBD(ZC_ZCBD zc)
        {
            await new RepositoryFactory().BaseRepository().Update(zc);

            if (zc.Zt == "2")
            {
                string sql = "begin ";
                sql += "declare @zcmc varchar(200);declare @zcdm varchar(50);declare @syr varchar(50);declare @sybmdm varchar(20);declare @sybmmc varchar(200);declare @cfdd varchar(200);";
                sql += "declare @syr1 varchar(50);declare @sybmdm1 varchar(20);declare @sybmmc1 varchar(200);declare @cfdd1 varchar(200);";
                sql += "select @zcdm=Zcdm,@zcmc=zcmc,@syr=Syr,@sybmdm=Sybmdm,@sybmmc=Sybmmc,@cfdd=Cfdd from ZC_ZCBDMX where Bdid='" + zc.ListID + "';";
                sql += "select @syr1=Syr,@sybmdm1=Sybmdm,@sybmmc1=Sybmmc,@cfdd1=Cfdd from ZC_ZCKP where Zcdm=@zcdm;";
                sql += " if isnull(@syr,'')<>'' begin ";
                sql += "Update ZC_ZCKP set Syr=@syr where Zcdm=@zcdm;";
                sql += "insert into ZC_ZCBDJL values('" + DateTime.Now.ToString("yyyyMMddHHmmssfffffff") + "','" + zc.ListID + "',@zcdm,@zcmc,'Syr','使用人变更',@syr1,@syr,getdate(),'资产变更'); end";
                sql += " if isnull(@sybmdm,'')<>'' begin ";
                sql += "Update ZC_ZCKP set Sybmdm=@sybmdm,Sybmmc=@sybmmc where Zcdm=@zcdm;";
                sql += "insert into ZC_ZCBDJL values('" + DateTime.Now.ToString("yyyyMMddHHmmssfffffff") + "','" + zc.ListID + "',@zcdm,@zcmc,'Sybmmc','使用部门变更',@sybmmc1,@sybmmc,getdate(),'资产变更'); end";
                sql += " if isnull(@cfdd,'')<>'' begin ";
                sql += "Update ZC_ZCKP set cfdd=@cfdd where Zcdm=@zcdm;";
                sql += "insert into ZC_ZCBDJL values('" + DateTime.Now.ToString("yyyyMMddHHmmssfffffff") + "','" + zc.ListID + "',@zcdm,@zcmc,'Cfdd','存放地点变更',@cfdd1,@cfdd,getdate(),'资产变更'); end";
                sql += " end";
                await new RepositoryFactory().BaseRepository().ExecuteSql(sql, null);
            }
        }

        public static async Task DeleteZC_ZCBD(string Listid)
        {
            //await new RepositoryFactory().BaseRepository().Update(zc);
            string sql = "update ZC_ZCBD set zt='0' where ListID='" + Listid + "';delete from ZC_ZCBDMX where Bdid='" + Listid + "'";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        }

        public static async Task<DataTable> GetZC_ZCBDByID(string listid)
        {

            string sql = "select * from ZC_ZCBD where ListID='" + listid + "'";

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);

        }
        public static async Task<DataTable> GetZC_ZCBDMXByID(string listid)
        {

            string sql = @"select Dwdm,Dwmc,Zcdl,a.Zcdm,a.Zcmc,Sgsl,Sfjyxzc ,Sgdj,Sgzj,Yt,Rksl,Rkdj,Rkzj,Skr ,Rksj,ZclbDm,Zclbmc,Qdfsdm,Qdfsmc
      , Qdrq, Gzrq, Zcdqr, Cgfsdm, Cgfsmc, Pp, Ggxh, Cpxlh, Sccj, Xss, Htbh, Fph, Bxjzrq, Lyd, Cpnd, Wwdjdm, Wwdjmc, Cbs, Cbrq, Dah, Trsyrq
      , Yjsynx, YjsyYf, Ysxmbh, Bz, Syqmj, Dymj, Ftmj, Cqxsdm, Cqxsmc, Qszm, Qsxzdm, Qsxzmc, Qszh, Qsnx, Qsrdm, Qsrmc, Rzrq, Syqlxdm, Syqlxmc
      , Dl, Tdjcdm, Tdjcmc, Zlwz, Jzlxdm, Jzlxmc, Czbk, Jz, Fczbk, Zwrzztdm, Zwrzztmc, Zwrzrq, Kjpzh, Zjztdm, Zjztmc, Sl, Zmjz, Jldw, Zjfsdm
      , Zjfsmc, Yz, Ljzj, Yjtys, Yjjcz, Yzjl, Yzje, Zjksqj, Zjjsqj, a.Sybmdm,a.Sybmmc,a.Syr,Glbmdm ,Glbmmc,a.Cfdd,Syzkdm,Syzkmc
                from ZC_ZCBDMX a left join ZC_ZCKP b on a.Zcdm = b.Zcdm where a.ListID='" + listid + "'";

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);

        }

        public static async Task<DataTable> GetZC_ZCBDMX(string listid)
        {

            string sql = @"select Dwdm,Dwmc,Zcdl,a.Zcdm,a.Zcmc,Sgsl,Sfjyxzc ,Sgdj,Sgzj,Yt,Rksl,Rkdj,Rkzj,Skr ,Rksj,ZclbDm,Zclbmc,Qdfsdm,Qdfsmc
      , Qdrq, Gzrq, Zcdqr, Cgfsdm, Cgfsmc, Pp, Ggxh, Cpxlh, Sccj, Xss, Htbh, Fph, Bxjzrq, Lyd, Cpnd, Wwdjdm, Wwdjmc, Cbs, Cbrq, Dah, Trsyrq
      , Yjsynx, YjsyYf, Ysxmbh, Bz, Syqmj, Dymj, Ftmj, Cqxsdm, Cqxsmc, Qszm, Qsxzdm, Qsxzmc, Qszh, Qsnx, Qsrdm, Qsrmc, Rzrq, Syqlxdm, Syqlxmc
      , Dl, Tdjcdm, Tdjcmc, Zlwz, Jzlxdm, Jzlxmc, Czbk, Jz, Fczbk, Zwrzztdm, Zwrzztmc, Zwrzrq, Kjpzh, Zjztdm, Zjztmc, Sl, Zmjz, Jldw, Zjfsdm
      , Zjfsmc, Yz, Ljzj, Yjtys, Yjjcz, Yzjl, Yzje, Zjksqj, Zjjsqj, a.Sybmdm,a.Sybmmc,a.Syr,Glbmdm ,Glbmmc,a.Cfdd,Syzkdm,Syzkmc,a.ListID
                from ZC_ZCBDMX a left join ZC_ZCKP b on a.Zcdm = b.Zcdm where Bdid='" + listid + "'";

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);

        }

        public static async Task<DataTable> GetZC_ZCBDJL(string Zcdm)
        {

            string sql = @"select * from ZC_ZCBDJL where Zcdm='" + Zcdm + "'";

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);

        }

        public static async Task InsertZC_ZCBDMX(ZC_ZCBDMX zc)
        {
            await new RepositoryFactory().BaseRepository().Insert(zc);

        }

        public static async Task UpdateZC_ZCBDMX(ZC_ZCBDMX zc)
        {
            await new RepositoryFactory().BaseRepository().Update(zc);
        }

        public static async Task DeleteZC_ZCBDMX(string Listid)
        {
            string sql = "delete from ZC_ZCBDMX where ListID='" + Listid + "'";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        }


        #endregion

        #region "资产出借"
        public static async Task<(DataTable, int)> GetZC_ZCCJ(ZC_ZCCJQuerry querry)
        {
            string str = " and 1=1 ";
            if (!string.IsNullOrEmpty(querry.Dwdm) && querry.Dwdm != "string")
            {
                str += "and Jydwdm='" + querry.Dwdm + "'";
            }
            if (!string.IsNullOrEmpty(querry.Bmdm) && querry.Bmdm != "string")
            {
                str += " and bmdm='" + querry.Bmdm + "'";
            }
            if (!string.IsNullOrEmpty(querry.Djbh) && querry.Djbh != "string")
            {
                str += " and djbh='%" + querry.Djbh + "%'";
            }
            if (!string.IsNullOrEmpty(querry.Zt) && querry.Zt != "string")
            {
                str += " and zt='" + querry.Zt + "'";
            }
            if (querry.Sbrqs != null && querry.Sbrqe != null)
            {
                if (querry.Sbrqe > querry.Sbrqs)
                {
                    str += " and (Sqrq>='" + querry.Sbrqs + "' and Sqrq<='" + querry.Sbrqe + "')";
                }
            }
            string sql = "select * from ZC_ZCCJ where dwdm in " + querry.DepartID + str;
            bool isAsc = true;
            if (querry.Sort.ToUpper() != "ASC")
            {
                isAsc = false;
            }
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null, querry.SortString, isAsc, querry.Size, querry.Current);

        }

        public static async Task InsertZC_ZCCJ(ZC_ZCCJ zc)
        {
            await new RepositoryFactory().BaseRepository().Insert(zc);

        }

        public static async Task InsertZC_CJ(ZC_CJEntity entity)
        {
            ZC_ZCCJ cj = entity.zccj;
            cj.ListID = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
            cj.Djbh = await CreateZCDM(cj.Bmdm, "CJ");
            await InsertZC_ZCCJ(cj);
            foreach (ZC_ZCCJMX mx in entity.zccjmx)
            {
                mx.ListID = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");

                string sql = "insert into ZC_ZCCJMX(ListID,Cjid,Zcdm,Zcmc,Cjsl,Jldw) values" +
                    "('" + mx.ListID + "','" + cj.ListID + "','" + mx.Zcdm + "','" + mx.Zcmc + "'," + mx.Cjsl + ",'" + mx.Jldw + "');";
                await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
            }
        }

        public static async Task UpdateZC_ZCCJ(ZC_ZCCJ zc)
        {
            await new RepositoryFactory().BaseRepository().Update(zc);
        }

        public static async Task DeleteZC_ZCCJ(string listid)
        {

            string sql = "update ZC_ZCCJ set zt='0' where ListID='" + listid + "';delete from ZC_ZCCJMX where Cjid='" + listid + "'";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        }

        public static async Task<DataTable> GetZC_ZCCJByID(string listid)
        {

            string sql = "select * from ZC_ZCCJ where ListID='" + listid + "'";

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);

        }

        /// <summary>
        /// 需要关联折旧信息表
        /// </summary>
        /// <param name="listid"></param>
        /// <returns></returns>
        public static async Task<DataTable> GetZC_ZCCJMX(string listid)
        {

            string sql = "select a.*,b.Zclbdm,Zclbmc,Ggxh,rksl,Yz,Zmjz,Ljzj,b.Jldw jl from ZC_ZCCJMX a left join ZC_ZCKP b on a.Zcdm=b.Zcdm where cjid='" + listid + "'";

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);

        }

        /// <summary>
        /// 需要关联折旧信息表
        /// </summary>
        /// <param name="listid"></param>
        /// <returns></returns>
        public static async Task<DataTable> GetZC_ZCCJMXBYID(string listid)
        {

            string sql = "select a.cjsl,a.jldw as jw,b.* from ZC_ZCCJMX a left join ZC_ZCKP b on a.Zcdm=b.Zcdm where a.ListID='" + listid + "'";

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);

        }

        public static async Task InsertZC_CJMMX(ListEntity list)
        {
            string sql = "";
            int index = 0;
            foreach (string cid in list.cid)
            {
                sql += "insert into ZC_ZCCJMX(ListID,Cjid,Zcdm,Zcmc,Cjsl,Jldw) select '" +
                    "" + DateTime.Now.ToString("yyyyMMddHHmmssfffffff") + index + "','" + list.pid + "',Zcdm,Zcmc,0,Jldw from ZC_ZCKP where zcdm='" + cid + "';";
            }
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        }
        public static async Task InsertZC_ZCCJMX(ZC_ZCCJMX zc)
        {
            await new RepositoryFactory().BaseRepository().Insert(zc);

        }

        public static async Task UpdateZC_ZCCJMX(ZC_ZCCJMX zc)
        {
            string sql = "Update ZC_ZCCJMX set Cjsl=" + zc.Cjsl + ",Jldw='" + zc.Jldw + "' where ListID='" + zc.ListID + "';";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        }

        public static async Task DeleteZC_ZCCJMX(string listid)
        {

            string sql = "delete from  ZC_ZCCJMX where ListID='" + listid + "';";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        }


        #endregion

        #region "资产处置"
        public static async Task<(IEnumerable<dynamic>, int)> GetZC_ZCCZ(ZC_ZCCZQuerry querry)
        {
            string str = " and 1=1 ";
            if (!string.IsNullOrEmpty(querry.dwdm) && querry.dwdm != "string")
            {
                str += "and Dwdm='" + querry.dwdm + "'";
            }
            if (!string.IsNullOrEmpty(querry.bmdm) && querry.bmdm != "string")
            {
                str += " and bmdm='" + querry.bmdm + "'";
            }
            if (!string.IsNullOrEmpty(querry.djbh) && querry.djbh != "string")
            {
                str += " and djbh='%" + querry.djbh + "%'";
            }
            if (!string.IsNullOrEmpty(querry.zt) && querry.zt != "string")
            {
                str += " and zt='" + querry.zt + "'";
            }
            if(!string.IsNullOrEmpty(querry.czyydm) && querry.czyydm!="string")
                {
                str += " and czyydm='" + querry.czyydm+"'";
            }
            if (querry.sbrqs != null && querry.sbrqe != null)
            {
                if (querry.sbrqe > querry.sbrqs)
                {
                    str += " and (Sqrq>='" + querry.sbrqs + "' and Sqrq<='" + querry.sbrqe + "')";
                }
            }
            string sql = "select * from ZC_ZCCZ where dwdm in " + querry.DepartID + str;
            bool isAsc = true;
            if (querry.Sort.ToUpper() != "ASC")
            {
                isAsc = false;
            }
            return await new RepositoryFactory().BaseRepository().FindList(sql, null, querry.SortString, isAsc, querry.Size, querry.Current);

        }

        public static async Task InsertZC_ZCCZ(ZC_ZCCZ zc)
        {
            await new RepositoryFactory().BaseRepository().Insert(zc);

        }

        public static async Task InsertZC_CZ(ZC_CZEntity entity)
        {
            ZC_ZCCZ cz = entity.zccz;
            cz.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
            cz.djbh = await CreateZCDM(cz.dwdm, "CZ");
            await InsertZC_ZCCZ(cz);
            foreach (ZC_ZCCZMX mx in entity.zcczmx)
            {
                mx.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
                string sql = "insert into ZC_ZCCZMX(ListID,Czid,Zcdm,Zcmc,Czsl,Czjz,Czfsdm,Czfsmc,Pmjf) values" +
                    "('" + mx.listid + "','" + cz.listid + "','" + mx.zcdm + "','" + mx.zcmc + "'," + mx.czsl + "," + mx.czjz + ",'" + mx.czfsdm + "','" + mx.czfsmc + "','" + mx.pmjf + "');";
                await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
            }
        }


        public static async Task UpdateZC_ZCCZ(ZC_ZCCZ zc)
        {
            await DeleteZC_ZCCZ(zc.listid);
            await InsertZC_ZCCZ(zc);
            //if (zc.zt == "2")
            //{
            //    string sql = "declare @zcdm varchar(50);declare @sl decimal(20,2);decimal @zcmc varchar(200);";
            //    sql += @"declare ColCur cursor for select zcdm,Czsl,zcmc From ZC_ZCCZMX where Czid = '" + zc.listid + @"';  
            //    open ColCur;
            //    FETCH NEXT FROM ColCur INTO @zcdm,@sl,@zcmc;
            //    WHILE(@@FETCH_STATUS = 0)
            //    BEGIN
            //        declare @ysl decimal(20,2);
            //        select @ysl=rksl where zcdm=@zcdm;
            //        update ZC_ZCKP set rksl=rksl-@sl where zcdm=@zcdm;
            //        insert into ZC_ZCBDJL('" + DateTime.Now.ToString("yyyyMMddHHmmssfffffff") + @"','" + zc.listid + @"',
            //        @zcdm,@zcmc,'rksl','入库数量',@ysl,@ysl-@sl,getdate(),'资产处置')
            //        FETCH NEXT FROM ColCur INTO @zcdm,@sl,@zcmc;
            //    END
            //    close ColCur;
            //    deallocate ColCur; ";
            //    await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
            // }
        }

        public static async Task UpdateZC_ZCCZZT(ListStatus status)
        {
            string sql = "update ZC_ZCCZ set zt='" + status.Zt + "' where ListID='" + status.ListID + "'";
            if (status.Zt == "3")
            {
                //sql += "declare @zcdm varchar(50);declare @sl decimal(20,2);decimal @zcmc varchar(200);";
                //sql += @"declare ColCur cursor for select zcdm,Czsl,zcmc From ZC_ZCCZMX where Czid = '" + status.ListID + @"';  
                //open ColCur;
                //FETCH NEXT FROM ColCur INTO @zcdm,@sl,@zcmc;
                //WHILE(@@FETCH_STATUS = 0)
                //BEGIN
                //    declare @ysl decimal(20,2);
                //    select @ysl=rksl where zcdm=@zcdm;
                //    update ZC_ZCKP set rksl=rksl-@sl where zcdm=@zcdm;
                //    insert into ZC_ZCBDJL('" + DateTime.Now.ToString("yyyyMMddHHmmssfffffff") + @"','" + status.ListID + @"',
                //    @zcdm,@zcmc,'rksl','入库数量',@ysl,@ysl-@sl,getdate(),'资产处置')
                //    FETCH NEXT FROM ColCur INTO @zcdm,@sl,@zcmc;
                //END
                //close ColCur;
                //deallocate ColCur; ";
                sql += @" begin
INSERT INTO suzhou.dbo.FA_BDD(GSDM, BDDBH, GDDM, BDLX, BDQNR, BDHNR, BMYY, BDRQ, JSR, JSRID, BZPZ, YSH, SXR, SXRID) select
         gsdm,(SELECT CASE WHEN MAX(BDDBH) IS NULL THEN 1 ELSE MAX(BDDBH) + 1 END FROM suzhou.dbo.FA_BDD WHERE GSDM = (select top 1 left(Zcdm,15) from ZC_ZCCZMX where Czid = '"+status.ListID+ @"')),GDDM,
'2',yz,ljzj,d.Czyymc,convert(varchar(10), czrq,112),'系统管理员','1','0','1','系统管理员','1' from vw_fa_kp a left
                                                     join
(select b.Zcdm,c.Czyymc,c.ListID,c.czrq from ZC_ZCCZMX b inner join ZC_ZCCZ c on b.czid = c.listid) d on a.GDDM = d.Zcdm where d.ListID = '" + status.ListID + @"';
                UPDATE suzhou.dbo.FA_KP SET YZX = '1' ,JSFS = (select Czyymc from ZC_ZCCZ where ListID = '"+ status.ListID + @"'), ZXR = '系统管理员',ZXRID = '1', ZXRQ = '"+DateTime.Today.ToString("yyyyMMdd")+@"'
        WHERE GDDM in (select Zcdm from ZC_ZCCZMX where Czid = '"+ status.ListID +@"')     
		end";
                await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
            }
        }

        public static async Task DeleteZC_ZCCZ(string Listid)
        {
            //await new RepositoryFactory().BaseRepository().Update(zc);
            string sql = "delete from ZC_ZCCZ  where ListID='" + Listid + "';";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        }

        public static async Task<IEnumerable<dynamic>> GetZC_ZCCZByID(string listid)
        {

            string sql = "select * from ZC_ZCCZ where ListID=" + listid;

            return await new RepositoryFactory().BaseRepository().FindList(sql, null);

        }

        /// <summary>
        /// 需要关联折旧信息表
        /// </summary>
        /// <param name="listid"></param>
        /// <returns></returns>
        public static async Task<DataTable> GetZC_ZCCZMX(string listid)
        {

            string sql = "select a.*,b.Zclbdm,Zclbmc,b.Yz from ZC_ZCCZMX a left join ZC_ZCKP b on a.Zcdm=b.Zcdm where Czid=" + listid;

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);

        }

        /// <summary>
        /// 需要关联折旧信息表
        /// </summary>
        /// <param name="listid"></param>
        /// <returns></returns>
        public static async Task<IEnumerable<dynamic>> GetZCCZMX(string listid)
        {

            string sql = "select *,gddm as zcdm,gdmc as zcmc from vw_fa_kp where gddm in (select zcdm from ZC_ZCCZMX where Czid=" + listid+")";

            return await new RepositoryFactory().BaseRepository().FindList(sql, null);

        }

        public static async Task<DataTable> GetKpBYdm(string zcdm)
        {
            string sql = "select * from vw_fa_kp where gddm='"+zcdm+"'";
            return await new RepositoryFactory().BaseRepository().FindTable(sql,null);
        }
        public static async Task InsertZC_ZCCZMX(ZC_ZCCZMX zc)
        {
            await new RepositoryFactory().BaseRepository().Insert(zc);

        }

        public static async Task UpdateZC_ZCCZMX(ZC_ZCCZMX zc)
        {
            await DeleteZC_ZCCZMX(zc.listid);
            await InsertZC_ZCCZMX(zc);
        }

        public static async Task DeleteZC_ZCCZMX(string Listid)
        {
            string sql = "delete from ZC_ZCCZMX where Listid='" + Listid + "'";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        }

        public static async Task DeleteZCCZMX(string czid)
        {
            string sql = "delete from ZC_ZCCZMX where czid='" + czid + "'";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        }
        #endregion

        #region "资产调拨"
        public static async Task<(DataTable, int)> GetZC_ZCDB(ZC_ZCDBQuerry querry)
        {
            string str = " and 1=1 ";
            if (!string.IsNullOrEmpty(querry.Dwdm) && querry.Dwdm != "string")
            {
                str += "and Dwdm='" + querry.Dwdm + "'";
            }
            if (!string.IsNullOrEmpty(querry.Jsdwdm) && querry.Jsdwdm != "string")
            {
                str += " and Jsdwdm='" + querry.Jsdwdm + "'";
            }

            if (!string.IsNullOrEmpty(querry.Zt) && querry.Zt != "string")
            {
                str += " and zt='" + querry.Zt + "'";
            }
            if (querry.Sbrqs != null && querry.Sbrqe != null)
            {
                if (querry.Sbrqe > querry.Sbrqs)
                {
                    str += " and (Sqrq>='" + querry.Sbrqs + "' and Sqrq<='" + querry.Sbrqe + "')";
                }
            }
            string sql = "select * from ZC_ZCDB where dwdm in " + querry.DepartID + str;
            bool isAsc = true;
            if (querry.Sort.ToUpper() != "ASC")
            {
                isAsc = false;
            }
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null, querry.SortString, isAsc, querry.Size, querry.Current);

        }

        public static async Task InsertZCDB(ZC_DB db)
        {
            db.zcdb.ListID = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
            db.zcdb.Djbh = await CreateZCDM(db.zcdb.Bmdm, "DB");
            await InsertZC_ZCDB(db.zcdb);
            foreach (ZC_ZCDBMX mx in db.listdb)
            {
                mx.Dbid = db.zcdb.ListID;
                mx.ListID = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
                string sql = "Insert into ZC_ZCDBMX(listid,Dbid,zcdm,zcmc,Czsl,Jsbmdm,Jsbmmc) " +
                    "values('" + mx.ListID + "','" + mx.Dbid + "','" + mx.Zcdm + "','" + mx.Zcmc + "'," + mx.Czsl + ",'" + mx.Jsbmdm + "','" + mx.Jsbmmc + "');";
                await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
            }
        }

        public static async Task InsertZC_ZCDB(ZC_ZCDB zc)
        {
            await new RepositoryFactory().BaseRepository().Insert(zc);

        }

        public static async Task UpdateZC_ZCDB(ZC_ZCDB zc)
        {
            await new RepositoryFactory().BaseRepository().Update(zc);
        }

        public static async Task UpdateZCDBZT(ListStatus status)
        {
            string sql = "update ZC_ZCDB set zt='" + status.Zt + "' where Listid='" + status.ListID + "'";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        }

        public static async Task DeleteZC_ZCDB(string Listid)
        {

            string sql = "update ZC_ZCDB set zt='0' where listid='" + Listid + "';delete from ZC_ZCDBMX where Dbid='" + Listid + "'";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        }

        public static async Task<DataTable> GetZC_ZCDBByID(string listid)
        {

            string sql = "select * from ZC_ZCDB where ListID=" + listid;

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);

        }

        /// <summary>
        /// 需要关联折旧信息表
        /// </summary>
        /// <param name="listid"></param>
        /// <returns></returns>
        public static async Task<DataTable> GetZC_ZCDBMX(string listid)
        {

            string sql = "select a.ListID,a.Zcdm,a.Zcmc,a.Czsl,a.Jsbmdm,a.Jsbmmc,b.ZclbDm,b.Zclbmc,b.Ggxh,b.Rksl,b.Jldw,b.yz,b.Ljzj,b.Zmjz,b.Jz from ZC_ZCDBMX a left join ZC_ZCKP b on a.Zcdm=b.Zcdm where Dbid='" + listid + "'";

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);

        }

        public static async Task<DataTable> GetZC_ZCDBMXBYid(string listid)
        {

            string sql = "select a.ListID as ID,a.Zcdm as dm,a.Zcmc as mc,a.Czsl,a.Jsbmdm,a.Jsbmmc,b.* from ZC_ZCDBMX a left join ZC_ZCKP b on a.Zcdm=b.Zcdm where a.listid='" + listid + "'";

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);

        }
        public static async Task InsertZC_ZCDBMX(ZC_ZCDBMX zc)
        {
            await new RepositoryFactory().BaseRepository().Insert(zc);

        }

        public static async Task UpdateZC_ZCDBMX(ZC_ZCDBMX zc)
        {
            string sql = "update ZC_ZCDBMX set Czsl=" + zc.Czsl + ",Jsbmdm='" + zc.Jsbmdm + "',Jsbmmc='" + zc.Jsbmmc + "' where ListID='" + zc.ListID + "';";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        }

        public static async Task DeleteZC_ZCDBMX(string Listid)
        {
            string sql = "delete from ZC_ZCDBMX where Listid='" + Listid + "'";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        }


        #endregion

        #region "资产调拨确认"
        public static async Task<(DataTable, int)> GetZC_ZCDBQr(ZC_DBQRQuerry querry)
        {
            string str = " 1=1 ";
            if (!string.IsNullOrEmpty(querry.Dwdm) && querry.Dwdm != "string")
            {
                str += "and Dwdm='" + querry.Dwdm + "'";
            }
            if (!string.IsNullOrEmpty(querry.Zcmc) && querry.Zcmc != "string")
            {
                str += " and Zcmc='" + querry.Zcmc + "'";
            }
            if (!string.IsNullOrEmpty(querry.Zclbdm) && querry.Zclbdm != "string")
            {
                str += " and Zclbdm='%" + querry.Zclbdm + "%'";
            }
            if (!string.IsNullOrEmpty(querry.Zclxdm) && querry.Zclxdm != "string")
            {
                str += " and Zclxdm='" + querry.Zclxdm + "'";
            }
            if (querry.Sqrqs != null && querry.Sqrqe != null)
            {
                if (querry.Sqrqe > querry.Sqrqs)
                {
                    str += " and (Sbrq>='" + querry.Sqrqs + "' and Sbrq<='" + querry.Sqrqe + "')";
                }
            }
            if (!string.IsNullOrEmpty(querry.Qrzt) && querry.Zclxdm != "Qrzt")
            {
                str += " and isnull(Rzt,'0')='" + querry.Qrzt + "'";
            }
            string sql = @"select a.*,c.Dwdm,c.Dwmc,b.ZclbDm,b.Zclbmc,c.Sbrq,b.Ggxh,b.Jldw,c.Bmdm,c.Bmmc
,(case when a.rzt = '1' then '已确认' else '未确认' end) as Rzts,(case when a.czt = '1' then '已确认' else '未确认' end) as Czts from ZC_ZCDBMX a left join ZC_ZCKP b on a.Zcdm = b.Zcdm left join ZC_ZCDB c on a.Dbid = c.ListID where " + str;
            bool isAsc = true;
            if (querry.Sort.ToUpper() != "ASC")
            {
                isAsc = false;
            }
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null, querry.SortString, isAsc, querry.Size, querry.Current);

        }

        public static async Task UpdateZC_ZCDBPRMXR(string listid)
        {
            string sql = "select * from ZC_ZCDB where ListID=(select Dbid from ZC_ZCDBMX where Listid='" + listid + "');";
            DataTable table = await new RepositoryFactory().BaseRepository().FindTable(sql, null);
            string bmdm = Convert.ToString(table.Rows[0]["Jsdwdm"]);
            string bmmc = Convert.ToString(table.Rows[0]["Jsdwmc"]);
            string zcdm = await CreateZCDM(bmdm, "");
            sql = "Update ZC_ZCDBMX set Rzt='1',Rdate=getdate() where ListID='" + listid + "';";
            sql += "declare @zcdm varchar(50);declare @sl decimal(20,2);declare @zcmc varchar(200);declare @rsl decimal(20,2);";
            sql += "select @zcdm=zcdm,@sl=Czsl,@zcmc=zcmc from ZC_ZCDBMX where ListID='" + listid + "';";
            sql += "select @rsl=Rksl from ZC_ZCKP where zcdm=@zcdm;";
            sql += "update ZC_ZCKP set Rksl=rksl-@sl where zcdm=@zcdm;";
            sql += "Insert into ZC_ZCBDJL values('" + DateTime.Now.ToString("yyyyMMddHHmmssfffffff") + "'," +
                "'" + listid + "',@zcdm,@zcmc,'Rksl','入库数量',isnull(@rsl,0),isnull(@rsl-@sl,0),getdate(),'资产调拨')";
            sql += @"INSERT INTO ZC_ZCKP
           (ListID, Dwdm, Dwmc, Zcdl, Zcdm, Zcmc, Sgsl, Sfjyxzc, Sgdj, Sgzj, Yt, Rksl, Rkdj, Rkzj, Skr
           , Rksj, ZclbDm, Zclbmc, Qdfsdm, Qdfsmc, Qdrq, Gzrq, Zcdqr, Cgfsdm, Cgfsmc, Pp, Ggxh, Cpxlh, Sccj
           , Xss, Htbh, Fph, Bxjzrq, Lyd, Cpnd, Wwdjdm, Wwdjmc, Cbs, Cbrq, Dah, Trsyrq, Yjsynx, YjsyYf
           , Ysxmbh, Bz, Syqmj, Dymj, Ftmj, Cqxsdm, Cqxsmc, Qszm, Qsxzdm, Qsxzmc, Qszh, Qsnx, Qsrdm, Qsrmc
           , Rzrq, Syqlxdm, Syqlxmc, Dl, Tdjcdm, Tdjcmc, Zlwz, Jzlxdm, Jzlxmc, Czbk, Jz, Fczbk, Zwrzztdm
           , Zwrzztmc, Zwrzrq, Kjpzh, Zjztdm, Zjztmc, Sl, Zmjz, Jldw, Zjfsdm, Zjfsmc, Yz, Ljzj, Yjtys, Yjjcz, Yzjl
           , Yzje, Zjksqj, Zjjsqj, Sybmdm, Sybmmc, Syr, Glbmdm, Glbmmc, Cfdd, Syzkdm, Syzkmc, ZdrID, Zdr, Zdrq, Zt)
            select '" + DateTime.Now.ToString("yyyyMMddHHmmssfffffff") + "','" + bmdm + "','" + bmmc + @"',Zcdl, Zcdm, Zcmc, Sgsl, Sfjyxzc, Sgdj, Sgzj, Yt, @sl, Rkdj, Rkzj, Skr
           , Rksj, ZclbDm, Zclbmc, Qdfsdm, Qdfsmc, Qdrq, Gzrq, Zcdqr, Cgfsdm, Cgfsmc, Pp, Ggxh, Cpxlh, Sccj
           , Xss, Htbh, Fph, Bxjzrq, Lyd, Cpnd, Wwdjdm, Wwdjmc, Cbs, Cbrq, Dah, Trsyrq, Yjsynx, YjsyYf
           , Ysxmbh, Bz, Syqmj, Dymj, Ftmj, Cqxsdm, Cqxsmc, Qszm, Qsxzdm, Qsxzmc, Qszh, Qsnx, Qsrdm, Qsrmc
           , Rzrq, Syqlxdm, Syqlxmc, Dl, Tdjcdm, Tdjcmc, Zlwz, Jzlxdm, Jzlxmc, Czbk, Jz, Fczbk, Zwrzztdm
           , Zwrzztmc, Zwrzrq, Kjpzh, Zjztdm, Zjztmc, Sl, Zmjz, Jldw, Zjfsdm, Zjfsmc, Yz, Ljzj, Yjtys, Yjjcz, Yzjl
           , Yzje, Zjksqj, Zjjsqj, Sybmdm, Sybmmc, Syr, Glbmdm, Glbmmc, Cfdd, Syzkdm, Syzkmc, ZdrID, Zdr, Zdrq, Zt 
            from ZC_ZCKP where zcdm=@zcdm;";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        }

        public static async Task UpdateZC_ZCDBPRMXC(string listid)
        {
            string sql = "Update ZC_ZCDBMX set Czt='1',Cdate=getdate() where ListID='" + listid + "'";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        }
        #endregion

        #region "资产盘点"
        public static async Task<(DataTable, int)> GetZC_ZCPD(ZC_ZCPDQuerry querry)
        {
            string str = " and 1=1 ";
            if (!string.IsNullOrEmpty(querry.Dwdm) && querry.Dwdm != "string")
            {
                str += "and Dwdm='" + querry.Dwdm + "'";
            }
            if (!string.IsNullOrEmpty(querry.Bmdm) && querry.Bmdm != "string")
            {
                str += " and bmdm='" + querry.Bmdm + "'";
            }
            if (!string.IsNullOrEmpty(querry.Djbh) && querry.Djbh != "string")
            {
                str += " and djbh='%" + querry.Djbh + "%'";
            }
            if (!string.IsNullOrEmpty(querry.Zt) && querry.Zt != "string")
            {
                str += " and zt='" + querry.Zt + "'";
            }

            if (querry.Fbrqs != null && querry.Fbrqe != null)
            {
                if (querry.Fbrqe > querry.Fbrqs)
                {
                    str += " and (Sqrq>='" + querry.Fbrqs + "' and Sqrq<='" + querry.Fbrqe + "')";
                }
            }
            string sql = "select * from ZC_ZCPD where dwdm in " + querry.DepartID + str;
            bool isAsc = true;
            if (querry.Sort.ToUpper() != "ASC")
            {
                isAsc = false;
            }
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null, querry.SortString, isAsc, querry.Size, querry.Current);

        }

        public static async Task InsertZC_ZCPD(ZC_ZCPD zc)
        {
            await new RepositoryFactory().BaseRepository().Insert(zc);

        }



        public static async Task UpdateZC_ZCPD(ZC_ZCPD zc)
        {
            await new RepositoryFactory().BaseRepository().Update(zc);
        }

        public static async Task UpdateZC_ZCPDZT(ListStatus status)
        {
            string sql = "Update ZC_ZCPD set zt='" + status.Zt + "' where Listid='" + status.ListID + "'";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        }

        public static async Task DeleteZC_ZCPD(string Listid)
        {
            //await new RepositoryFactory().BaseRepository().Update(zc);
            string sql = "update ZC_ZCPD set zt='0' where ListID='" + Listid + "'";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        }

        public static async Task<DataTable> GetZC_ZCPDByID(string listid)
        {

            string sql = "select * from ZC_ZCPD where ListID=" + listid;

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);

        }

        /// <summary>
        /// 需要关联折旧信息表
        /// </summary>
        /// <param name="listid"></param>
        /// <returns></returns>
        public static async Task<DataTable> GetZC_ZCPDMX(string listid)
        {

            string sql = "select * from ZC_ZCPDMX where Pdid='" + listid + "'";

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);

        }

        /// <summary>
        /// 需要关联折旧信息表
        /// </summary>
        /// <param name="listid"></param>
        /// <returns></returns>
        public static async Task<DataTable> GetZC_ZCPDMXByID(string Listid)
        {

            string sql = "select * from ZC_ZCPDMX where Listid='" + Listid + "'";

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);

        }
        public static async Task InsertZC_ZCPDMX(ZC_ZCPDMX zc)
        {
            await new RepositoryFactory().BaseRepository().Insert(zc);

        }

        public static async Task UpdateZC_ZCPDMX(ZC_ZCPDMX zc)
        {
            await new RepositoryFactory().BaseRepository().Update(zc);
        }

        public static async Task DeleteZC_ZCPDMX(string Listid)
        {
            string sql = "delete from ZC_ZCPDMX where Listid='" + Listid + "'";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        }


        #endregion

        #region "盘点结果处理"
        public static async Task<(DataTable, int)> GetZC_ZCPDCL(ZC_ZCPDZCQuerry querry)
        {
            string str = " 1=1 ";
            if (!string.IsNullOrEmpty(querry.Dwdm) && querry.Dwdm != "string")
            {
                str += "and Dwdm='" + querry.Dwdm + "'";
            }
            if (!string.IsNullOrEmpty(querry.Zcmc) && querry.Zcmc != "string")
            {
                str += " and Zcmc like '%" + querry.Zcmc + "%'";
            }
            if (!string.IsNullOrEmpty(querry.Bmdm) && querry.Bmdm != "string")
            {
                str += " and Bmdm= '" + querry.Bmdm + "'";
            }
            if (!string.IsNullOrEmpty(querry.Pdzt) && querry.Pdzt != "string")
            {
                str += " and Pdzt= '" + querry.Pdzt + "'";
            }

            if (!string.IsNullOrEmpty(querry.Zt) && querry.Zt != "string")
            {
                str += " and Zt= '" + querry.Zt + "'";
            }
            if (querry.Fbrqs != null && querry.Fbrqe != null)
            {
                if (querry.Fbrqe > querry.Fbrqs)
                {
                    str += " and (Fbrq>='" + querry.Fbrqs + "' and Sbrq<='" + querry.Fbrqe + "')";
                }
            }

            string sql = @"select a.*,b.Dwdm,b.Dwmc,b.Bmdm,b.Bmmc,b.Zt as Pzt,b.Fbrq from ZC_ZCPDMX a left join ZC_ZCPD b on a.Pdid=b.ListID where " + str;
            bool isAsc = true;
            if (querry.Sort.ToUpper() != "ASC")
            {
                isAsc = false;
            }
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null, querry.SortString, isAsc, querry.Size, querry.Current);

        }

        public static async Task UpdateZC_ZCPDCL(string listid, string userid, string username, string zt)
        {
            string sql = "Update ZC_ZCPDMX set Zt='" + zt + "',ShrID='" + userid + "',Shr='" + username + "',Shrq=getdate() where ListID='" + listid + "'";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
            if (zt == "2")
            {
                sql = "begin declare @pdsl decimal(20,2); declare @zcdm varchar(50);declare @zcmc varchar(200);declare @ysl decimal(20,2)";
                sql += "select @pdsl=sl,@zcdm=zcdm,@zcmc=zcmc from ZC_ZCPDMX where listid='" + listid + "';";
                sql += "select @ysl=sl from ZC_ZCKP where zcdm=@zcdm;";
                sql += "Update ZC_ZCKP set sl=@pdsl where zcdm=@zcdm;";
                sql += "Insert into ZC_ZCBDJL values('" + DateTime.Now.ToString("yyyyMMddHHmmssfffffff") + "','" + listid + "',@zcdm,@zcmc,'Sl','数量',@ysl,@pdsl,getdate(),'资产盘点');";
                sql += " end";
                await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
            }
        }
        #endregion

        #region "经营性资产"


        public static async Task<(DataTable, int)> GetZC_JYXZC(ZC_JYXZCQuerry querry)
        {
            string str = " and 1=1 ";
            if (!string.IsNullOrEmpty(querry.Dwdm) && querry.Dwdm != "string")
            {
                str += "and Dwdm='" + querry.Dwdm + "'";
            }
            if (!string.IsNullOrEmpty(querry.Bmdm) && querry.Bmdm != "string")
            {
                str += " and Bmdm='" + querry.Bmdm + "'";
            }
            if (!string.IsNullOrEmpty(querry.Zcmc) && querry.Zcmc != "string")
            {
                str += " and zcmc like '%" + querry.Zcmc + "%'";
            }
            if (!string.IsNullOrEmpty(querry.Zclxdm) && querry.Zclxdm != "string")
            {
                str += " and Zclxdm like '%" + querry.Zclxdm + "%'";
            }

            string sql = "select * from ZC_JYXZC where dwdm in " + querry.DepartID + str;
            bool isAsc = true;
            if (querry.Sort.ToUpper() != "ASC")
            {
                isAsc = false;
            }
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null, querry.SortString, isAsc, querry.Size, querry.Current);

        }

        public static async Task InsertZC_JYXZC(ZC_JYXZC zc)
        {
            await new RepositoryFactory().BaseRepository().Insert(zc);

        }

        public static async Task UpdateZC_JYXZC(ZC_JYXZC zc)
        {
            await new RepositoryFactory().BaseRepository().Update(zc);
        }

        //public static async Task DeleteZC_JYXZC(ZC_JYXZC zc)
        //{
        //    await new RepositoryFactory().BaseRepository().Update(zc);
        //}

        public static async Task<DataTable> GetZC_JYXZCByID(string listid)
        {

            string sql = "select a.*,b.zt from ZC_JYXZC a left join ZC_ZCKP b on a.zcdm=b.zcdm where a.ListID='" + listid + "'";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);

        }


        #endregion

        #region "经营性资产部位"


        public static async Task<DataTable> GetZC_ZCBW(string zcdm)
        {

            string sql = "select * from ZC_ZCBW where zcdm='" + zcdm + "'";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);

        }

        public static async Task InsertZC_ZCBW(ZC_ZCBW zc)
        {
            await new RepositoryFactory().BaseRepository().Insert(zc);

        }

        public static async Task UpdateZC_ZCBW(ZC_ZCBW zc)
        {
            await new RepositoryFactory().BaseRepository().Update(zc);
        }

        public static async Task DeleteZC_ZCBW(string Listid)
        {
            string sql = "Update ZC_ZCBW set Zt='0' where ListID='" + Listid + "'";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        }

        public static async Task<DataTable> GetZC_ZCBWByID(string listid)
        {

            string sql = "select * from ZC_ZCBW where ListID='" + listid + "'";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);

        }


        #endregion

        #region "资产合同"

        #endregion

        #region “折旧”
        public static async Task<DataTable> GetZC_ZCZJ(string Dwdm, string Yf)
        {

            string sql = "select * from ZC_YZJ where dwdm='" + Dwdm + "' and jtyf='" + Yf + "'";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);

        }

        public static async Task<string> CreateZC_YZJ(string Dwdm, string Yf, string Userid)
        {
            string str = "";
            string sql = "select * from ZC_YZJ where dwdm='" + Dwdm + "' and jtyf='" + Yf + "'";
            DataTable dataTable = await new RepositoryFactory().BaseRepository().FindTable(sql, null);
            if (dataTable.Rows.Count > 0)
            {
                str = "该单位在会计期间" + Yf + "已计提折旧，不能重复计提！";
                return str;
            }
            sql = @"insert into ZC_YZJ select '" + Yf + @"' as yf,a.dwdm,a.Zcdm,a.Yz,b.Jczl,a.Yz*b.Jczl,a.Ljzj,(a.Yz-a.Ljzj-(a.Yz*b.Jczl))/(a.YjsyYf-a.Yjtys),
                (a.Yz - a.Ljzj - (a.Yz * b.Jczl)) / (a.YjsyYf - a.Yjtys) / (a.Yz - (a.Yz * b.Jczl)),a.Yjtys + 1,
                a.YjsyYf,'" + Userid + "',getdate() from ZC_ZCKP a left join ZC_CGML b on a.ZclbDm = b.Mldm where a.dwdm='" + Dwdm + "';";
            sql += "update ZC_YZJ set Ljzj=ljzj+yzj;";
            sql += "update ZC_ZCKP set Ljzj=(select ljzj from ZC_YZJ a on a.zcdm=zcdm and a.dwdm='" + Dwdm + "' and jtyf='" + Yf + "');";
            sql += "update ZC_ZCKP set Yjtys=(select yjsyyf from ZC_YZJ a on a.zcdm=zcdm and a.dwdm='" + Dwdm + "' and jtyf='" + Yf + "');";
            sql += "update ZC_ZCKP set Yjjcz=(select jcz from ZC_YZJ a on a.zcdm=zcdm and a.dwdm='" + Dwdm + "' and jtyf='" + Yf + "');";
            sql += "update ZC_ZCKP set Yzjl=(select yzjv from ZC_YZJ a on a.zcdm=zcdm and a.dwdm='" + Dwdm + "' and jtyf='" + Yf + "');";
            sql += "update ZC_ZCKP set Yzje=(select Yzje from ZC_YZJ a on a.zcdm=zcdm and a.dwdm='" + Dwdm + "' and jtyf='" + Yf + "');";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
            return str;
        }

        public static async Task DeleteZC_YZJ(string Dwdm, string Yf)
        {
            DateTime date = Convert.ToDateTime(Yf.Substring(0, 4) + "-" + Yf.Substring(4) + "-01").AddMonths(-1);
            string syf = date.ToString("yyyyMM");
            string sql = "begin update ZC_ZCKP set Ljzj=(select ljzj from ZC_YZJ a on a.zcdm=zcdm and a.dwdm='" + Dwdm + "' and jtyf='" + syf + "');";
            sql += "update ZC_ZCKP set Yjtys=(select yjsyyf from ZC_YZJ a on a.zcdm=zcdm and a.dwdm='" + Dwdm + "' and jtyf='" + syf + "');";
            sql += "update ZC_ZCKP set Yjjcz=(select jcz from ZC_YZJ a on a.zcdm=zcdm and a.dwdm='" + Dwdm + "' and jtyf='" + syf + "');";
            sql += "update ZC_ZCKP set Yzjl=(select yzjv from ZC_YZJ a on a.zcdm=zcdm and a.dwdm='" + Dwdm + "' and jtyf='" + syf + "');";
            sql += "update ZC_ZCKP set Yzje=(select Yzje from ZC_YZJ a on a.zcdm=zcdm and a.dwdm='" + Dwdm + "' and jtyf='" + syf + "');";
            sql += "delete from ZC_YZJ where dwdm='" + Dwdm + "' and jtyf='" + Yf + "'; end";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        }

        #endregion


        #region "地块"


        public static async Task<(IEnumerable<dynamic>, int)> GetZC_DK(ZC_DKQuerry querry)
        {



            string sql = @"select distinct a.*,c.orgName,d.orgName as jdmc,e.orgName as sssq from ZC_DK a
            left join Org c on a.dwdm = c.org
left join Org d on left(a.dwdm,9)= d.org left join Org e on left(a.dwdm,12)= e.org where 1=1 and dwdm like '" + querry.DepartID + "%'";
            if (!string.IsNullOrEmpty(querry.dkmc))
            {
                sql += " and dkmc like '%" + querry.dkmc + "%'";
            }
            if (!string.IsNullOrEmpty(querry.dwdm))
            {
                //sql += " and dwdm like '"+querry.dwdm+"%'";
                string ss = "";
                foreach (string s1 in querry.dwdm.Split(","))
                {
                    if (!string.IsNullOrEmpty(s1))
                    {
                        ss += " dwdm like '" + s1 + "%' or";
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 3);
                    // sql += " and b.ss in (" + ss + ")";
                    sql += " and (" + ss + ")";
                }
            }
            if (!string.IsNullOrEmpty(querry.sxdm))
            {
                string ss = "";
                foreach (string s1 in querry.sxdm.Split(","))
                {
                    if (!string.IsNullOrEmpty(s1))
                    {
                        ss += "'" + s1 + "',";
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);
                    // sql += " and b.ss in (" + ss + ")";
                    sql += " and sxdm in(" + ss + ")";
                }
            }
            if (!string.IsNullOrEmpty(querry.lbdm))
            {
                string ss = "";
                foreach (string s1 in querry.lbdm.Split(","))
                {
                    if (!string.IsNullOrEmpty(s1))
                    {
                        ss += "'" + s1 + "',";
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);
                    sql += " and lbmc in (" + ss + ")";
                }
            }
            if (querry.xzle >= querry.xzls)
            {
                sql += " and (xzl>=" + querry.xzls + " and xzl<=" + querry.xzle + ")";
            }

            bool isAsc = true;
            if (querry.Sort.ToUpper() != "ASC")
            {
                isAsc = false;
            }
            return await new RepositoryFactory().BaseRepository().FindList(sql, null, "listid", false, querry.Size, querry.Current);

        }


        public static async Task<IEnumerable<dynamic>> GetZC_DKsum(ZC_DKQuerry querry)
        {



            string sql = @"select COUNT(1) as count,SUM(isnull(yz,0)) as yz,sum(tdmj) as tdmj,sum(jzmj) as jzmj,sum(kymj) as kymj,sum(zcsl) as zcsl from ZC_DK where 1=1 and dwdm like '" + querry.DepartID + "%'";
            if (!string.IsNullOrEmpty(querry.dkmc))
            {
                sql += " and dkmc like '%" + querry.dkmc + "%'";
            }
            if (!string.IsNullOrEmpty(querry.dwdm))
            {
                //sql += " and dwdm like '"+querry.dwdm+"%'";
                string ss = "";
                foreach (string s1 in querry.dwdm.Split(","))
                {
                    if (!string.IsNullOrEmpty(s1))
                    {
                        ss += " dwdm like '" + s1 + "%' or";
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 3);
                    // sql += " and b.ss in (" + ss + ")";
                    sql += " and (" + ss + ")";
                }
            }
            if (!string.IsNullOrEmpty(querry.sxdm))
            {
                string ss = "";
                foreach (string s1 in querry.sxdm.Split(","))
                {
                    if (!string.IsNullOrEmpty(s1))
                    {
                        ss += "'" + s1 + "',";
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);
                    // sql += " and b.ss in (" + ss + ")";
                    sql += " and sxdm in(" + ss + ")";
                }
            }
            if (!string.IsNullOrEmpty(querry.lbdm))
            {
                string ss = "";
                foreach (string s1 in querry.lbdm.Split(","))
                {
                    if (!string.IsNullOrEmpty(s1))
                    {
                        ss += "'" + s1 + "',";
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);
                    sql += " and lbmc in (" + ss + ")";
                }
            }
            if (querry.xzle >= querry.xzls)
            {
                sql += " and (xzl>=" + querry.xzls + " and xzl<=" + querry.xzle + ")";
            }

          
            return await new RepositoryFactory().BaseRepository().FindList(sql, null);

        }

        public static async Task InsertZC_DK(ZC_DK zc)
        {
            await new RepositoryFactory().BaseRepository().Insert(zc);
            if (zc.jzmj > 0)
            {
                string sql = "update ZC_DK set xzl=kymj/jzmj*100 where listid='" + zc.listid + "'";
                await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
            }
        }

        public static async Task UpdateZC_DK(ZC_DK zc)
        {
            DeleteZC_DK(zc.listid);
            await InsertZC_DK(zc);
        }

        public static async Task DeleteZC_DK(string Listid)
        {
            string sql = "delete from  ZC_DK where ListID='" + Listid + "';";
            sql += "delete from zc_dkzc where dkid='"+Listid+"'";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);

        }

        public static async Task UpdateDKstatus(string Listid, string zt)
        {
            string sql = "update  ZC_DK set zt='" + zt + "' where ListID='" + Listid + "'";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        }
        public static async Task InsertZC_DKZC(DKZC zc)
        {
            string sql = "Insert into zc_dkzc values('" + zc.listid + "','" + zc.dkid + "','" + zc.zcbm + "')";
            //await new RepositoryFactory().BaseRepository().Insert(zc);
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        }
        //public static async Task UpdateZC_DKZC(ZC_DKZC zc)
        //{
        //    await DeleteZC_DKZC(zc.dkid);
        //    await InsertZC_DKZC(zc);
        //}

        public static async Task DeleteZC_DKZC(string Listid)
        {
            string sql = "delete from  ZC_DKZC where dkid='" + Listid + "'";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        }

        public static async Task<DataTable> GetZC_DKByID(string listid)
        {

            string sql = "select * from ZC_DK where ListID='" + listid + "'";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);

        }

        public static async Task<DataTable> GetZC_DKZCByID(string listid)
        {

            string sql = @"select distinct a.*,b.org,b.orgName,c.org as area,c.orgName as areaname,
(case when isnull(f.assetcode, '') = '' and k.ZKDM = '01' then '闲置' when isnull(f.assetcode, '') = '' and k.ZKDM <> '01' then '其他' else '出租' end ) as ztmc,
(case when isnull(f.assetcode, '') = '' and k.ZKDM = '01' then '1' when isnull(f.assetcode, '') = '' and k.ZKDM <> '01' then '3' else '2' end ) as zt,
(case when a.zclx = '固定资产' then(a.jzmj - isnull(f.tradearea, 0)) else (a.zymj - isnull(tradearea, 0)) end) as kymj,h.text as dwlx,j.text as dwsx,k.ZKDM,k.ZKMC,f.tradearea,
 convert(decimal(23, 4), (case when(case when a.zclx = '固定资产' then(a.jzmj - isnull(f.tradearea, 0)) else (a.zymj - isnull(tradearea, 0)) end)> 0 then
         (case when a.zclx = '固定资产' then(a.jzmj - isnull(f.tradearea, 0)) else (a.zymj - isnull(tradearea, 0)) end)/
                (case when a.zclx = '固定资产' then a.jzmj else a.zymj end) else 0 end))*100 as xzl
from Asset a left
join Org b on left(zcbm, 15) = b.org and b.isshow = '1' left
  join Org c on left(zcbm, 9) = c.org and c.isshow = '1'
left
join(select assetcode, SUM(tradearea) as tradearea from VW_AssetArea a left
                                                   join Contract b on a.projcode = b.projcode
where b.contractenddate >= GETDATE()
                                                   group by assetcode) f
                on a.zcbm = f.assetcode left join DicInfo h on b.lh = h.value and h.item = 'orgsx' left join DicInfo j on b.ss = j.value and j.item = 'orgfl' left join
vw_fa_kp k on a.zcbm = k.GDDM where zcbm in (select zcbm from zc_dkzc where dkid='" + listid + "')";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);

        }



        #endregion

        #region 
        public static async Task<(DataTable,int)> GetZCKP(string dwdm,string dep,string bmmc,string lbmc,string zcmc,string zcdm,int size,int Current)
        {


            string str = "";
            if (!string.IsNullOrEmpty(dwdm))
            {
                str = " and gsdm='" + dwdm + "'";
            }
            if (!string.IsNullOrEmpty(bmmc))
            {
                str += " and bmmc like '%" + bmmc + "%'";
            }
            if (!string.IsNullOrEmpty(lbmc))
            {
                str += " and lbmc like '%"+lbmc+"%'";
            }
            if (!string.IsNullOrEmpty(zcmc))
            {
                str += " and gdmc like '%"+zcmc+"%'";
            }
            if (!string.IsNullOrEmpty(zcdm))
            {
                str += " and gddm like '%" + zcdm + "%'";
            }
            string sql = @"select *,gddm as zcdm,gdmc as zcmc from vw_fa_kp where gdsl=1 and  gsdm like '" + dep +"%'"+ str;
           
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null,"gddm", false, size,Current);

        }

        public static async Task<DataTable> GetJSFS()
        {
            string sql = "SELECT distinct zjdm,zjfs FROM suzhou.dbo.FA_Zjfs where zjdm like '2%' and zjdm <>'2' order by zjdm";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }

        #endregion
    }
}
